实体框架 - 具有不匹配列的父子关系

时间:2015-08-29 23:41:01

标签: entity-framework

是否可以与两个具有复合键的实体建立父/子关系,其中列名不匹配?

例如。 表A的复合关键字段是(CustNmbr,SiteId) 表B的复合关键字段是(Account,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; }

}

1 个答案:

答案 0 :(得分:0)

当然, 请参阅下面的代码(我简化了您的实体):

映射:

modelBuilder.Entity<Customer>()
    .HasKey(t => new { t.CustomerNumber, t.SiteId }) //composite key
    .HasMany(t => t.Contracts)
    .WithRequired(t => t.Customer);

modelBuilder.Entity<ContractData>() // child entity
    .HasKey(c => new { c.ContractNumber, c.Account }) // composite key
    .HasRequired(c => c.Customer) // parent relation
    .WithMany()
    .Map(c => c.MapKey("column1", "column2")); //mapping to different column names

实体:

public class ContractData
{
    public virtual string ContractNumber { get; set; }
    public virtual string Account { get; set; }
    public virtual Customer Customer { get; set; }
}

public class Customer
{
    public Customer()
    {
        Contracts = new List<ContractData>();
    }

    public virtual string CustomerNumber { get; set; }
    public virtual int SiteId { get; set; }
    public virtual ICollection<ContractData> Contracts { get; set; }
    public virtual string Name { get; set; }
}

底层数据库:

Customer          1 <---> n   ContractData
-----------------             -----------------
CustomerNumber PK             column1        FK
SiteId         PK             column2        FK
Name                          ContractNumber PK
                              Account        PK