簡體   English   中英

實體框架雙方都需要關聯嗎?

[英]Does Entity Framework need associations on both sides?

我有以下模型:

public class ProductGroup
{
    public int Id {get; set;}
    public string Name {get; set;}
}

public class Product
{
    public int Id {get; set;}
    public int ProductGroupId {get; set;}
    public string Name {get; set;}
    public virtual ProductGroup ProductGroup {get; set;}
}

什么是在正確的CodeFirst映射ProductGroup在需要Product

並且在映射中,我需要為ProductGroup指定映射,還是可以只設置:

this.Property(t => t.ProductGroupId)
    .HasColumnName("productgroup_id")
    .IsRequired();

映射所需關系的正確方法是:

this.HasRequired(p=>p.ProductGroup).WithMany().HasForeignKey(p=>p.ProductGroupId);

我假設您正在Product實體的配置類中配置此關系

暫無
暫無

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

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