EF代码第一关系

时间:2013-08-05 06:19:02

标签: asp.net entity-framework asp.net-mvc-4

有人可以告诉我如何在我的EF codefirst示例中创建一个关系 - 我想在Products类上建立一个与Product_Spec类有很多关系的关系,所以当我编译代码时,它会在生成数据库时产生关系,以及与Product_Spec相关的Specification类的关系 数据上下文类

类:

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);
        }
    }
}

2 个答案:

答案 0 :(得分: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;}        
}

答案 1 :(得分:0)

您似乎正在尝试在ProductsSpecifications之间创建多人关系。如果是这种情况,您不需要使用默认约定来定义Product_Spec,实体框架将为您创建所需的联结表,前提是您对实体进行了一些更改(以定义关系)。

在您的情况下,您可以进行以下更改:

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的表格,这是用于保存多种Product/Specification关系的联结表。

如果您需要明确定义此映射(例如,如果您有现有表),您应该能够执行以下操作:

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