2012-02-22 10 views
4
public class Attribute 
{ 
    [Key] 
    public int AttributeId { get; set; } 

    [Required, StringLength(100)] 
    public string Name { get; set; } 

    public int ValueAttributeId { get; set; } 
    public Attribute ValueAttribute { get; set; } 

    public IList<Attribute> ValueAttributes { get; set; } 
} 

    modelBuilder.Entity<Attribute>() 
    .HasOptional(a => a.ValueAttribute) 
    .WithMany(a => a.ValueAttributes) 
    .HasForeignKey(a => a.ValueAttributeId); 

で、オプションの自己参照1対多の関係を定義します:参照制約を持つ多重衝突を役割に「Attribute_ValueAttribute_Target」の関係に「Attribute_ValueAttribute」を。依存ロールのすべてのプロパティはnull不可能なので、プリンシパルロールの多重度は「1」でなければなりません。はtSystem.Data.Entity.Edm.EdmAssociationType 流暢アピ

Aaaaahhhh .....

+0

public int? ValueAttributeId {get;セット; } –

+2

EFCFを使用する利点の一部として、 '[Key]'や '[Required]'のような属性でPOCOを修飾する必要はありません。 – Yuck

+0

はい...私のドメインからのEFの参照を避けることができました。 –

答えて

10
public int ? ValueAttributeId { get; set; } 

... nullでできるようにするために必要なプロパティ。