与 EF 4.1 Code First 的一对多关系 - 使用数据库而不是触发器
我们首先正在研究 EF 代码,以评估它是否适合我们现有的数据库。数据库实体的结构为
1) Product (Composite Key)
int Product ID: PK (Non Identity)- 自动生成而不是触发器 int 版本来自:PK(非身份)- 在代替触发器中自动生成
2) Pack(复合密钥)
PackID:PK(非身份)- 在代替触发器中自动生成 版本来自:PK(非身份)- 自动生成而不是触发 产品 ID:(不能设置为 FK - 设计约束)
关系:产品有很多包
我们如何使用 EF Code First 4.1 来模拟上述场景?
尝试过的解决方案
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public short Version { get; set; }
public virtual ICollection<Pack> Packs { get; set; }
}
public class Pack
{
public int PackID { get; set; }
public int ProductID { get; set; }
public short Version { get; set; }
public virtual Product Product { get; set; }
}
public class ProductContext : DbContext
{
public DbSet<Pack> Pack { get; set; }
public DbSet<Product> Product { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().ToTable("Product");
modelBuilder.Entity<Pack>().ToTable("Pack");
modelBuilder.Entity<Product>()
.HasKey(a => new { a.ProductID, a.VersionFrom });
modelBuilder.Entity<Pack>()
.HasKey(a => new { a.PackID, a.VersionFrom });
modelBuilder.Entity<Product>().HasMany<Pack>(x => x.Packs).WithRequired().HasForeignKey(p => p.ProductID);
base.OnModelCreating(modelBuilder);
}
}
....
var product = new Product { ProductName = "EntTest1"};
var pack = new Pack {};
using (var productContext = new ProductContext())
{
product.Packs.Add(pack);
productContext.Product.Add(product);
productContext.SaveChanges(); //**ERROR**
}
....
在模型生成过程中检测到一个或多个验证错误:
System.Data.Edm.EdmAssociationConstraint: : Number of Properties in the Dependent and Principal Role in a relationship constraint must be exactly identical.
请帮忙!!!
We are working on EF code first to evaluate if it fits to our existing database. The structure of the database entites are
1) Product (Composite Key)
int Product ID: PK (Non Identity)- Autogenerated in Instead Of Trigger
int Version From: PK (Non Identity)- Autogenerated in Instead Of Trigger
2) Pack (Composite Key)
PackID : PK (Non Identity)- Autogenerated in Instead Of Trigger
Version From: PK (Non Identity)- Autogenerated in Instead Of Trigger
Product ID: (Cannot be set as FK - Design Constraint)
RelationShip: Product has many packs
How can we model the above scenario with EF Code First 4.1?
The solution which tried
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public short Version { get; set; }
public virtual ICollection<Pack> Packs { get; set; }
}
public class Pack
{
public int PackID { get; set; }
public int ProductID { get; set; }
public short Version { get; set; }
public virtual Product Product { get; set; }
}
public class ProductContext : DbContext
{
public DbSet<Pack> Pack { get; set; }
public DbSet<Product> Product { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().ToTable("Product");
modelBuilder.Entity<Pack>().ToTable("Pack");
modelBuilder.Entity<Product>()
.HasKey(a => new { a.ProductID, a.VersionFrom });
modelBuilder.Entity<Pack>()
.HasKey(a => new { a.PackID, a.VersionFrom });
modelBuilder.Entity<Product>().HasMany<Pack>(x => x.Packs).WithRequired().HasForeignKey(p => p.ProductID);
base.OnModelCreating(modelBuilder);
}
}
....
var product = new Product { ProductName = "EntTest1"};
var pack = new Pack {};
using (var productContext = new ProductContext())
{
product.Packs.Add(pack);
productContext.Product.Add(product);
productContext.SaveChanges(); //**ERROR**
}
....
One or more validation errors were detected during model generation:
System.Data.Edm.EdmAssociationConstraint: : Number of Properties in the Dependent and Principal Role in a relationship constraint must be exactly identical.
Please HELP!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
Pack
类需要两个外键标量属性您需要向映射提供两个标量属性
编辑: 不映射标量属性
您的
Pack(s)
表应包含具有匹配数据类型的ProductID
、ProductVersion
列。Your
Pack
class need two foreign key scalar propertiesYou need to supply both scalar properties to the mapping
Edit: Without mapping scalar properties
Your
Pack(s)
table should haveProductID
,ProductVersion
columns with matching data types.