与 EF 4.1 Code First 的一对多关系 - 使用数据库而不是触发器

发布于 2024-12-21 05:34:31 字数 2160 浏览 3 评论 0原文

我们首先正在研究 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江心雾 2024-12-28 05:34:31

您的 Pack 类需要两个外键标量属性

public class Pack
{
        public int PackID { get; set; }
        public int ProductID { get; set; }
        public short ProductVersion { get; set; }
        public virtual Product Product { get; set; }
}

您需要向映射提供两个标量属性

modelBuilder.Entity<Product>()
.HasMany<Pack>(x => x.Packs).WithRequired(p => p.Product)
.HasForeignKey(p => new { p.ProductID, p.ProductVersion});

编辑: 不映射标量属性

public class Pack
{
        public int PackID { get; set; }
        public virtual Product Product { get; set; }
}

modelBuilder.Entity<Product>()
.HasMany<Pack>(x => x.Packs).WithRequired(p => p.Product)
.Map(m => m.MapKey("ProductID", "ProductVersion"));

您的 Pack(s) 表应包含具有匹配数据类型的 ProductIDProductVersion 列。

Your Pack class need two foreign key scalar properties

public class Pack
{
        public int PackID { get; set; }
        public int ProductID { get; set; }
        public short ProductVersion { get; set; }
        public virtual Product Product { get; set; }
}

You need to supply both scalar properties to the mapping

modelBuilder.Entity<Product>()
.HasMany<Pack>(x => x.Packs).WithRequired(p => p.Product)
.HasForeignKey(p => new { p.ProductID, p.ProductVersion});

Edit: Without mapping scalar properties

public class Pack
{
        public int PackID { get; set; }
        public virtual Product Product { get; set; }
}

modelBuilder.Entity<Product>()
.HasMany<Pack>(x => x.Packs).WithRequired(p => p.Product)
.Map(m => m.MapKey("ProductID", "ProductVersion"));

Your Pack(s) table should have ProductID, ProductVersion columns with matching data types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文