4

自動生成されたクラスをマップする必要がありますが、明らかな理由で変更したくありません(自動生成されています!)。

では、Entity Frameworkの流暢なAPI(私の場合は4.3)を使用して主キーを定義する可能性はありますか?属性をComplexTypeとして処理すると、NULL値が原因で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

You can use HasKey method.

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