ef-fluent-api

Entity Framework Core 2.0: How to configure abstract base class once

99封情书 提交于 2019-11-26 21:34:48
问题 I have a base model: public abstract class Status { public string updateUserName { get; set; } } Then a model which extends the base model defined above: public class Item : Status { public int Id { get; set; } public string Description { get; set; } } Then I have defined configuration classes for each: public class ItemConfiguration : IEntityTypeConfiguration<Item> { public void Configure(EntityTypeBuilder<Item> builder) { builder.ToTable("Item", "dbo").HasKey(c => c.Id); builder.Property(c

How to define Many-to-Many relationship through Fluent API Entity Framework?

南楼画角 提交于 2019-11-26 18:58:29
问题 Below is my model: public class TMUrl { //many other properties //only property with type Keyword public List<Keyword> Keywords{get;set;} } public class Keyword { //many other properties //only property with type TMUrl public List<TMUrl> Urls{get;set;} } So clearly, both the entities have many-to-many relationship. I chose fluent api to tell the entity-framework about this relationship i.e. modelBuilder.Entity<TMUrl> .HasMany(s => s.Keywords) .WithMany(s => s.URLs).Map(s => { s.MapLeftKey(

Entity Framework Code First - Advantages and disadvantages of Fluent Api vs Data Annotations [closed]

匆匆过客 提交于 2019-11-26 18:16:28
When creating a database using Entity Framework code-first, a lot of the database model is can be extracted from the code. Fluent API and/or Attributes can be used to fine tune the model. What are the advantages and disadvantages of Fluent Api in comparison to Data Annotations? In other words: Even if in certain situations both methods can be used, in what cases should one method prevail above the other? Everything what you can configure with DataAnnotations is also possible with the Fluent API. The reverse is not true. So, from the viewpoint of configuration options and flexibility the Fluent

Setting unique Constraint with fluent API?

江枫思渺然 提交于 2019-11-26 18:10:25
I'm trying to build an EF Entity with Code First, and an EntityTypeConfiguration using fluent API. creating primary keys is easy but not so with a Unique Constraint. I was seeing old posts that suggested executing native SQL commands for this, but that seem to defeat the purpose. is this possible with EF6? Yorro On EF6.2 , you can use HasIndex() to add indexes for migration through fluent API. https://github.com/aspnet/EntityFramework6/issues/274 Example modelBuilder .Entity<User>() .HasIndex(u => u.Email) .IsUnique(); On EF6.1 onwards, you can use IndexAnnotation() to add indexes for

Entity Framework Code first: cycles or multiple cascade paths

社会主义新天地 提交于 2019-11-26 14:32:38
问题 I have a Booking class that has a booking contact (a Person ) and a set of navigation properties ( People ) that links through a join table to another set of navigation properties ( Bookings ) in Person . How do I generate the Booking table with cascading deletes enabled for the booking contact relationship? When I leave it out of the fluent API code (default setting of cascade delete enabled) I get the following error message from migration: Introducing FOREIGN KEY constraint 'FK_dbo

One to one optional relationship using Entity Framework Fluent API

冷暖自知 提交于 2019-11-26 12:54:40
We want to use one to one optional relationship using Entity Framework Code First. We have two entities. public class PIIUser { public int Id { get; set; } public int? LoyaltyUserDetailId { get; set; } public LoyaltyUserDetail LoyaltyUserDetail { get; set; } } public class LoyaltyUserDetail { public int Id { get; set; } public double? AvailablePoints { get; set; } public int PIIUserId { get; set; } public PIIUser PIIUser { get; set; } } PIIUser may have a LoyaltyUserDetail but LoyaltyUserDetail must have a PIIUser . We tried these fluent approach techniques. modelBuilder.Entity<PIIUser>()

How to do a join in linq to sql with method syntax?

依然范特西╮ 提交于 2019-11-26 11:36:43
I have seen lots of examples in LINQ to SQL examples on how to do a join in query syntax but I am wondering how to do it with method syntax? For example how might I do the following var result = from sc in enumerableOfSomeClass join soc in enumerableOfSomeOtherClass on sc.Property1 equals soc.Property2 select new { SomeClass = sc, SomeOtherClass = soc } with a .Join() ? Can anyone illustrate or provide another simple example? Justin Niessner var result = from sc in enumerableOfSomeClass join soc in enumerableOfSomeOtherClass on sc.Property1 equals soc.Property2 select new { SomeClass = sc,

Entity Framework Code First - Advantages and disadvantages of Fluent Api vs Data Annotations [closed]

爱⌒轻易说出口 提交于 2019-11-26 06:14:14
问题 When creating a database using Entity Framework code-first, a lot of the database model is can be extracted from the code. Fluent API and/or Attributes can be used to fine tune the model. What are the advantages and disadvantages of Fluent Api in comparison to Data Annotations? In other words: Even if in certain situations both methods can be used, in what cases should one method prevail above the other? 回答1: Everything what you can configure with DataAnnotations is also possible with the

Setting unique Constraint with fluent API?

僤鯓⒐⒋嵵緔 提交于 2019-11-26 06:11:50
问题 I\'m trying to build an EF Entity with Code First, and an EntityTypeConfiguration using fluent API. creating primary keys is easy but not so with a Unique Constraint. I was seeing old posts that suggested executing native SQL commands for this, but that seem to defeat the purpose. is this possible with EF6? 回答1: On EF6.2 , you can use HasIndex() to add indexes for migration through fluent API. https://github.com/aspnet/EntityFramework6/issues/274 Example modelBuilder .Entity<User>() .HasIndex

One to one optional relationship using Entity Framework Fluent API

非 Y 不嫁゛ 提交于 2019-11-26 03:08:40
问题 We want to use one to one optional relationship using Entity Framework Code First. We have two entities. public class PIIUser { public int Id { get; set; } public int? LoyaltyUserDetailId { get; set; } public LoyaltyUserDetail LoyaltyUserDetail { get; set; } } public class LoyaltyUserDetail { public int Id { get; set; } public double? AvailablePoints { get; set; } public int PIIUserId { get; set; } public PIIUser PIIUser { get; set; } } PIIUser may have a LoyaltyUserDetail but