4

我需要映射一些自动生成的类,出于明显的原因我不想更改它们(它是自动生成的!)。

那么是否有可能使用实体框架的流畅 API(在我的情况下为 4.3)定义主键?由于 NULL 值,将属性作为 ComplexType 处理会导致 DbUpdateExcetion。向生成的类添加主键可能是一种解决方案,但不适合我,POCO 必须保持不变。请帮忙!谢谢。

目前我忽略了属性,因为...

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.

这是一个缩短的示例代码:

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

1 回答 1

6

您可以使用HasKey方法。

builder.Entity<FooBar>().HasKey(...);
于 2012-05-23T19:21:29.670 回答