コードファーストの CTP 機能を備えた EF4 を使用して、単純な複合パターン エンティティ モデルを作成しています。
public abstract partial class CacheEntity
{
[Key]public string Hash { get; set; }
public string Creator { get; set; }
public int EntityType { get; set; }
public string Name { get; set; }
public string Predecessor { get; set; }
public DateTime DateTimeCreated { get; set; }
public virtual ICollection<CacheReference> References { get; set; }
}
public partial class CacheBlob : CacheEntity
{
public byte[] Content { get; set; }
}
public partial class CacheCollection : CacheEntity
{
public virtual ICollection<CacheEntity> Children { get; set; }
}
public class CacheReference
{
public string Hash { get; set; }
[Key]public string Reference { get; set; }
public virtual CacheEntity Entity { get; set; }
}
public class CacheEntities : DbContext
{
public DbSet<CacheEntity> Entities { get; set; }
public DbSet<CacheReference> References { get; set; }
}
プリミティブ/コレクション派生クラスを分割する前は、すべてうまく機能していましたが、今では次のようになります。
Unable to determine the principal end of the 'Cache.DataAccess.CacheEntity_References'
relationship. Multiple added entities may have the same primary key.
混乱している可能性があると考えたので、DataAnnotation 属性ではなく、流暢なインターフェイスを使用して明示的に綴ろうと思いました。関係を適切に定義していると私が考えるものは次のとおりです。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<CacheEntity>().HasKey(ce => ce.Hash);
modelBuilder.Entity<CacheEntity>().HasOptional(ce => ce.References).WithMany();
modelBuilder.Entity<CacheReference>().HasKey(ce => ce.Reference);
modelBuilder.Entity<CacheReference>().HasRequired(cr => cr.Entity).WithOptional();
}
しかし、私は間違っているに違いありません。
Entities in 'CacheEntities.CacheReferenceSet' participate in the
'CacheReference_Entity' relationship. 0 related 'Entity' were found. 1 'Entity' is expected.
流暢な API を使用する他のさまざまな方法では、さまざまなエラーが発生しますが、何も成功しません。そのため、継承を使用しているときにこれらを別の方法で行う必要があるかどうか疑問に思い始めています。
手がかり、リンク、アイデア、ガイダンスは大歓迎です。