1
是否可以与列名不匹配的具有复合键的两个实体具有父/子关系?实体框架 - 与非匹配列的父子关系
例如。 表A的复合键字段(CustNmbr,SITEID) 表B的复合键字段(帐号,SiteNumber)
我已经试过各种映射似乎并不能使它发挥作用。根据我读过的内容,您应该能够在模型中将其映射出来。我试着和相当悲惨地失败了:
public DbSet<Customer> Customers { get; set; }
public DbSet<Customer_Contract_Data> Contracts { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Configure the primary Key for the OfficeAssignment
modelBuilder.Entity<Customer>()
.HasKey(t => new { t.CustNmbr, t.SiteId });
modelBuilder.Entity<Customer>()
.HasRequired(t => t.Contracts)
.WithMany()
.HasForeignKey(t => new { t.CustNmbr, t.SiteId });
}
// function that retursn all orders
private IQueryable<Customer> getCustomers()
{
// return the data
return Customers;
}
public List<Customer> GetCustomers()
{
return getCustomers().ToList();
}
// function that retursn all orders
public Customer GetCustomer(Int32 siteId, string custNbr)
{
// return the data
return getCustomers().Include(m=>m.Contracts).FirstOrDefault(x => x.CustNmbr == custNbr && x.SiteId == siteId); ;
}
// dispose
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
}
这里有阶级结构
[Table("CustomerData")]
public class Customer
{
[Key, Column(Order=2)]
public string CustNmbr { get; set; }
[Key, Column(Order=1)]
public Int32 SiteId { get; set; }
public string GPCompany { get; set; }
public string CustName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string CCode { get; set; }
public string Zip { get; set; }
public string Phone1 { get; set; }
public string Fax { get; set; }
public DateTime DateUploadedFromLive { get; set; }
public virtual ICollection<Customer_Contract_Data> Contracts { get; set; }
public Customer() { }
}
[Table("Customer_Contract_Data")]
public class Customer_Contract_Data
{
[Key]
public string Contractnumber { get; set; }
public Int32 SiteNumber { get; set; }
public string Account { get; set; }
public string Contracttype { get; set; }
public string Contracttypedescription { get; set; }
public string Servicetype { get; set; }
public string Country { get; set; }
public string Zipcode { get; set; }
public DateTime Contractstartdate { get; set; }
public DateTime Contractenddate { get; set; }
public virtual Customer Customer { get; set; }
}
非常感谢你的回复。我在这一块上徘徊了一下。这看起来不错。干杯! –
不客气。试试看,让我知道如果它是正确的。 – Tomas