How to add a primary key with a smooth API - Entity Framework EF 4.x Code First

I need to display some automatically generated classes that I do not want to change for obviouse (it is automatically generated!).

So, is it possible to define a primary key with the full API of the Entity Framework (4.3 in my case)? Processing attributes like ComplexType result in a DbUpdateExcetion due to NULL values. Adding a primary key to the generated classes may be a solution, but not suitable for me, POCOs should remain unchanged. Please help! Thanks.

I am currently ignoring attributes due to ...

ModelValidationException EntityType 'Attribute' has no key defined. Define the key for this EntityType. EntitySet 'Attributes' is based on type 'Attribute' that has no keys defined. 

here is an example code example:

 public class Item { public ID {set;get;} public Attribute[] Attributes {set;get;} } public class Attribute { public SomeComplexType misc1 {set; get;} public SomeComplexType misc2 {set; get;} } public class ItemDb : DbContext { public ItemDb(string connection) : base(connection) {} public DbSet<Item> Items { get; set; } protected override void OnModelCreating(DbModelBuilder builder) { // currently I am ignoring the Attributes builder.Ignore<Attributes>(); } } 
+4
source share
1 answer

You can use the HasKey method .

 builder.Entity<FooBar>().HasKey(...); 
+4
source

Source: https://habr.com/ru/post/1413935/


All Articles