首先使用不同的列名配置一对一映射EF代码。流畅的API

时间:2015-12-09 14:10:05

标签: c# entity-framework

我有两个实体:

public partial class Web_Vendor
{  
    public string VendorID { get; set; }
    public virtual Deal_Brands DealBrand { get; set; }
}
 public partial class Deal_Brands
 {    
    public string Brand { get; set; }    
    public virtual Web_Vendor WebVendor { get; set; }    
 }

我需要使用字段VendorID <-> Brand使用流畅的API配置一对一映射。

modelBuilder.Entity<Web_Vendor>().HasOptional(x=>x.DealBrand).WithRequired(x=>x.WebVendor).
                Map(x=>x.MapKey(""))

任何人都可以帮忙继续这条线吗?

1 个答案:

答案 0 :(得分:2)

查看你的课程,我发现Deal_BrandsDeal_Brands之间的关系是“一对一”(1-0.1)而不是“一对一”。这意味着Web_Vendor主键必须是public partial class Web_Vendor { public string VendorID { get; set; } public virtual Deal_Brands DealBrand { get; set; } } public partial class Deal_Brands { public string VendorID { get; set; } public string Brand { get; set; } public virtual Web_Vendor WebVendor { get; set; } } 主键。这些类将是这样的:

modelBuilder.Entity<Web_Vendor>()
    .HasKey(i => i.VendorID);

modelBuilder.Entity<Deal_Brands>()
    .HasKey(i => i.VendorID);

modelBuilder.Entity<Web_Vendor>()
     .HasOptional(x=>x.DealBrand)
     .WithRequired(x=>x.WebVendor);

映射:

acl content_present req_len gt 0
tcp-request content accept if content_present

希望它有所帮助!