2013-08-05 561 views
1

有人可以告訴我如何在我的EF代碼第一個示例中創建關係 - 我想要一個與Product_Spec類有很多關係的Products類的關係,所以當我編譯代碼時它將與數據庫生成,也爲相關Product_Spec 數據上下文類EF代碼第一關係

類的規範類的關係:

namespace MvcApplication1.Models 
{ 
    public class Department 
    { 
     [Key] 
     public long Id { get; set; } 

     [Required(ErrorMessage="Please enter a name for the departments.")] 
     [DataType(DataType.Text)] 
     public string Name { get; set; } 

     [DataType(DataType.Text)] 
     [Required(ErrorMessage = "Please enter a valid url for the department.")] 
     public string Url { get; set; } 

     public virtual List<Product> Products { get; set; } 
    } 


    public class Product 
    { 
     [Key] 
     public long Id { get; set; } 
     [ForeignKey("FK_Department_Id")] 
     public long DepartmentId { get; set; } 
     [DataType(DataType.Text)] 
     public string Title { get; set; } 

     [DataType(DataType.Currency)] 
     public decimal UnitPrice { get; set; } 
     [DataType(DataType.Currency)] 
     public decimal SellPrice { get; set; }    

    } 

    public class Product_Spec 
    {   
     [ForeignKey("FK_Spec_ProductId")] 
     public long ProductId { get; set; } 

     [ForeignKey("FK_Spec_SpecId")] 
     public long SpecId { get; set; }   
    } 

    public class Specification 
    { 
     [Key] 
     public long SpecId { get; set; } 
     [DataType(DataType.Text)] 
     [Required(ErrorMessage = "Please enter a product specification type.")] 
     public string Type { get; set; } 
     [DataType(DataType.Text)] 
     [Required(ErrorMessage = "Please enter a product specification value.")] 
     public string Value { get; set; } 

    } 


} 


namespace MvcApplication1 
{ 

    public class DataContext : DbContext 
    { 
     public DbSet<Department> Department { get; set; } 
     public DbSet<Product> Product { get; set; } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      modelBuilder.Entity<Department>().HasRequired(x => x.Products) 
       .WithMany().HasForeignKey(x => x.Id).WillCascadeOnDelete(true); 

      modelBuilder.Entity<Product>().HasOptional(x => x.Product_Specs) 
       .WithMany().HasForeignKey(x =>x.ProductId) // this lines doesn't work 

      base.OnModelCreating(modelBuilder); 
     } 
    } 
} 

回答

0

我想你應該在ForeignKey屬性設置列名,而不是約束名稱:

public class Product 
{ 
    [Key] 
    public long Id { get; set; } 
    public long DepartmentId { get; set; } 

    [DataType(DataType.Text)] 
    public string Title { get; set; } 

    [DataType(DataType.Currency)] 
    public decimal UnitPrice { get; set; } 

    [DataType(DataType.Currency)] 
    public decimal SellPrice { get; set; } 

    [ForeignKey("DepartmentId")] 
    public virtual Department Department { get; set; } 

    public ICollection<Product_Spec> ProductSpecs { get; set; } 
} 

public class Product_Spec 
{     
    public long ProductId { get; set; }   
    public long SpecId { get; set; } 

    [ForeignKey("ProductId")] 
    public virtual Product Product {get; set;}   
} 
0

看起來您正試圖創建ProductsSpecifications之間的多對多關係。如果是這種情況,則不需要使用默認約定來定義Product_Spec,如果您對實體進行了一些更改(以定義關係),Entity Framework將爲您創建所需的聯結表。

在你的情況,你可以做如下改變:

public class Product 
{ 
    // Your other code 

    // [ForeignKey("FK_Department_Id")] - Not required, EF will configure the key using conventions 
    public long DepartmentId { get; set; }   

    public virtual ICollection<Specification> Specifications { get; set; } // Navigation property for one end for your Product *..* Specification relationship. 
} 

public class Specification 
{ 
    // Your other code 
    public virtual ICollection<Product> Products { get; set; } 
} 

在創建你的表,你會看到一個表像SpecificationProducts一個名稱,它是用來裝你的許多路口表。 .many Product/Specification關係。

如果您需要明確定義此映射(例如,如果你有一個現有的表),你應該能夠做這樣的事情:

modelBuilder.Entity<Product>(). 
    HasMany(s => s.Specifications). 
    WithMany(p => p.Products). 
    Map(
    m => 
    { 
    m.MapLeftKey("ProductId"); 
    m.MapRightKey("SpecId"); 
    m.ToTable("SpecificationProducts"); 
    });