Asp.net EF代码优先关系
有人能告诉我如何在我的EF codefirst示例中创建关系吗?我希望Products类上的关系与Product_Spec类有很多关系,因此当我编译代码时,在生成数据库时,它将具有关系,以及与Product_Spec相关的Specification类的关系 数据上下文类 课程:Asp.net EF代码优先关系,asp.net,entity-framework,asp.net-mvc-4,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 { ge
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);
}
}
}
namespace mvcapapplication1.Models
{
公共课系
{
[关键]
公共长Id{get;set;}
[必需(ErrorMessage=“请输入部门名称”。)]
[数据类型(DataType.Text)]
公共字符串名称{get;set;}
[数据类型(DataType.Text)]
[必需(ErrorMessage=“请输入部门的有效url。”)]
公共字符串Url{get;set;}
公共虚拟列表产品{get;set;}
}
公共类产品
{
[关键]
公共长Id{get;set;}
[外键(“FK_部门Id”)]
公共长部门ID{get;set;}
[数据类型(DataType.Text)]
公共字符串标题{get;set;}
[数据类型(数据类型.货币)]
公共十进制单价{get;set;}
[数据类型(数据类型.货币)]
公共十进制SellPrice{get;set;}
}
公共类产品规格
{
[ForeignKey(“FK_Spec_ProductId”)]
公共长ProductId{get;set;}
[外键(“FK_Spec_SpecId”)]
公共长SpecId{get;set;}
}
公共类规范
{
[关键]
公共长SpecId{get;set;}
[数据类型(DataType.Text)]
[必需(ErrorMessage=“请输入产品规格类型。”)]
公共字符串类型{get;set;}
[数据类型(DataType.Text)]
[必需(ErrorMessage=“请输入产品规格值。”)]
公共字符串值{get;set;}
}
}
命名空间MVCAPApplication1
{
公共类DataContext:DbContext
{
公共数据库集部门{get;set;}
公共DbSet乘积{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().HasRequired(x=>x.Products)
.WithMany().HasForeignKey(x=>x.Id).WillCascadeOnDelete(true);
modelBuilder.Entity().has可选(x=>x.Product_规格)
.WithMany().HasForeignKey(x=>x.ProductId)//此行不起作用
基于模型创建(modelBuilder);
}
}
}
我认为您应该在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;}
}
公共类产品
{
[关键]
公共长Id{get;set;}
公共长部门ID{get;set;}
[数据类型(DataType.Text)]
公共字符串标题{get;set;}
[数据类型(数据类型.货币)]
公共十进制单价{get;set;}
[数据类型(数据类型.货币)]
公共十进制SellPrice{get;set;}
[外键(“部门ID”)]
公共虚拟部门部门{get;set;}
公共ICollection ProductSpecs{get;set;}
}
公共类产品规格
{
公共长ProductId{get;set;}
公共长SpecId{get;set;}
[外键(“产品ID”)]
公共虚拟产品产品{get;set;}
}
看起来您正试图在产品
和规范
之间建立一种多方面的关系。如果是这种情况,您不需要定义产品规格
,使用默认约定,“实体框架”将为您创建所需的连接表,前提是您对实体进行一些更改(以定义关系)
在您的情况下,您可以进行以下更改:
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; }
}
modelBuilder.Entity<Product>().
HasMany(s => s.Specifications).
WithMany(p => p.Products).
Map(
m =>
{
m.MapLeftKey("ProductId");
m.MapRightKey("SpecId");
m.ToTable("SpecificationProducts");
});