4

I need to map some auto generated classes, which I dont want to change for obviouse reasions (it's auto generated!).

So is there a possibility to define a Primary Key with fluent API of the Entity Framework (4.3 in my case)? Handling Attributes as ComplexType leads to DbUpdateExcetion due to NULL values. Adding a Primary Key to the generated classes may be a solution, but not suitable for me, POCOs must stay unchanged. Pleas help! Thanks.

Currently I am ignoring the Attributes because of the...

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 a shortend sample code:

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>();
    }
}

1 Answer 1

6

You can use HasKey method.

builder.Entity<FooBar>().HasKey(...);
Sign up to request clarification or add additional context in comments.

4 Comments

only on existing properties as I know
Yes. Beucase every entity in Entity Framework must have a key property.
@cincura.net: Hi, it is the property of EF to have primary key for each table? I mean i can't have table without primary key in EF?
Yes. Every entity needs to have a key. BTW this is a completely different question. Maybe better to ask new, I'll reply there ASAP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.