实体框架中的一对多主/外键

时间:2011-09-22 06:04:54

标签: c# .net entity-framework entity-framework-4.1 fluent-interface

如何使用Fluent API在以下entites上映射一对多关系。

[Table("LU_COMMODITY_ID")]
public class LU_COMMODITY_ID 
{
    [Key]
    public string COMMODITY_ID { get; set; }
    public virtual string COMMODITY_DESC { get; set; }
    ...

    public virtual ICollection<LU_SUPPLIER_COMMODITY> LU_SUPPLIER_COMMODITIES { get; set; }
}

[Table("LU_SUPPLIER_COMMODITY")]
public class LU_SUPPLIER_COMMODITY
{
    [Key, Column(Order = 0)] 
    public string COMMODITY_ID { get; set; }
    [Key, Column(Order = 1)] 
    public virtual string SUPPLIER_NAME { get; set; }
    ...
    public virtual LU_COMMODITY_ID LU_COMMODITY_ID  {get; set; }
}

我尝试了以下映射:

modelBuilder.Entity<LU_SUPPLIER_COMMODITY>() 
           .HasRequired(l => l.LU_COMMODITY_ID)
           .WithMany(a => a.LU_SUPPLIER_COMMODITIES)
           .HasForeignKey(l => l.COMMODITY_ID);

它给了我下一个错误:

  

“LU_SUPPLIER_COMMODITIES”属性不存在或未针对“LU_COMMODITY_ID”类型进行映射。

0 个答案:

没有答案