1

有人可以告诉我如何在我的 EF codefirst 示例中创建关系 - 我希望 Products 类上的关系与 Product_Spec 类有很多关系,所以当我编译代码时,它会在生成数据库时产生关系,而且与 Product_Spec Data Context 类相关的 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);
        }
    }
}
4

2 回答 2

0

Products看起来您正在尝试在和之间创建多对多关系Specifications。如果是这种情况,您不需要定义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为 的表,它是用于保存多..多Product/Specification关系的连接表。

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

modelBuilder.Entity<Product>().
  HasMany(s => s.Specifications).
  WithMany(p => p.Products).
  Map(
   m =>
   {
     m.MapLeftKey("ProductId");
     m.MapRightKey("SpecId");
     m.ToTable("SpecificationProducts");
   });
于 2013-08-05T11:52:20.057 回答
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;}        
}
于 2013-08-05T06:27:58.357 回答