簡體   English   中英

在EF 4.1中再添加一個導航類型

[英]Same navigation types one more in EF 4.1

我對下面的實體關系有點困惑。 因為它比一個用戶關系更多。 我覺得有些不對勁。 這有什么不對嗎?

public class Subject: Entity
{
    public Advert()
    {
        CreateDate = DateTime.Now;
    }
    public virtual User Owner{ get; set; }
    public virtual List<User> Voters{ get; set; }
    public virtual List<User> Followers{ get; set; }
}

我不確定這是否適用於您的列表,這應該是EF CodeFirst標准的ICollections。

您可能還希望使用繼承來區分不同類型的用戶,或者為關注者和選民使用不同的實體。

你需要使用WithMany()映射它

這將允許您為關系指定foreignKey

http://weblogs.asp.net/manavi/archive/2011/01/23/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations.aspx

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<User>()
                .HasRequired(a => a.BillingAddress)
                .WithMany()
                .HasForeignKey(u => u.BillingAddressId);

    modelBuilder.Entity<User>()
                .HasRequired(a => a.DeliveryAddress)
                .WithMany()
                .HasForeignKey(u => u.DeliveryAddressId);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM