简体   繁体   English

实体框架-可能在外键中使用复合主键的一部分

[英]Entity Framework - Possible to have part of compound primary key used in foreign key

I am trying to fit Entity Framework 5 to a legacy database where 2 tables both define a compound primary key. 我正在尝试将Entity Framework 5适配于旧数据库,其中2个表都定义了复合主键。

Product(ProductId, VersionId, Name, StrengthId) <- ProductId and VersionId form the PK
Strength(StrengthId, VersionId, Description) <- StrengthId and VersionId form the PK

There is a foreign key between the 2 tables defined as Product.StrengthId and Product.VersionID relates to Strength.StrengthId and Strength.VersionId which means that the foreign key uses part of the compound primary key of both tables. 在定义为Product.StrengthId和Product.VersionID的两个表之间有一个外键,它与Strength.StrengthId和Strength.VersionId相关,这意味着外键使用这两个表的复合主键的一部分。

I set up my mappings as follows: 我将映射设置如下:

    public ProductConfiguraton()
    {
        ToTable("t_Product");
        HasKey(p => new { p.Id, p.VersionId });

        HasRequired(p => p.Strength)
            .WithMany(b => b.Products)
            .Map(m => m.MapKey("iStrengthID", "iVersionID"));

        Property(p => p.Id).HasColumnName("iProductId");
        Property(p => p.Name).HasColumnName("sName");
        //Property(p => p.VersionId).HasColumnName("iVersionID");

    }

    public StrengthConfiguration()
    {
        ToTable("t_Strength");
        HasKey(p =>  new { p.Id, p.VersionId });

        Property(p => p.Id).HasColumnName("iStrengthID");
        Property(p => p.VersionId).HasColumnName("iVersionID");
        Property(p => p.Description).HasColumnName("sStrengthText");
    }

That produces the following create table script fragments: 这将产生以下创建表脚本片段:

CREATE TABLE IF NOT EXISTS t_LIVE_Product (
  iD INTEGER NOT NULL,
  VersionId INTEGER NOT NULL,
  sName text NULL,
  iStrengthID INTEGER NOT NULL,
  iVersionID INTEGER NOT NULL,
  PRIMARY KEY (iD, VersionId),
  FOREIGN KEY (iStrengthID, iVersionID) REFERENCES t_LIVE_Strength (iStrengthID, iVersionID) ON DELETE CASCADE
);

-- Index for foreign key FK_Product_Strength
CREATE INDEX IX_FK_Product_Strength ON t_LIVE_Product (iStrengthID, iVersionID);

CREATE TABLE IF NOT EXISTS t_LIVE_Strength ( 
  iStrengthID INTEGER NOT NULL,
  iVersionID INTEGER NOT NULL,
  sStrengthText text NULL,
  PRIMARY KEY (iStrengthID, iVersionID)
);

The Product table has created 2 columns for VersionId (VersionId and iVersionID), which is not what I want. 产品表已为VersionId(VersionId和iVersionID)创建了2列,这不是我想要的。 If I uncomment the last line in my ProductConfiguration which I get the following error: 如果取消注释ProductConfiguration的最后一行,则会出现以下错误:

error 0019: Each property name in a type must be unique. 错误0019:类型中的每个属性名称都必须是唯一的。 Property name 'iVersionID' was already defined. 属性名称“ iVersionID”已定义。

This makes sense, but leaves me no closer to solving my problem. 这是有道理的,但让我离解决问题越来越近了。

Is what I am attempting to do possible or does anyone know of any workarounds to try and fit this schema into EF? 我正在尝试做些什么,还是有人知道有什么解决方法可以尝试将此模式适合EF?

I'm not sure, but you can try this. 我不确定,但是您可以尝试一下。

public ProductConfiguraton()
{
    ToTable("t_Product");
    HasKey(p => new { p.Id, p.VersionId });

    HasRequired(p => new {p.VersionId, p.StrengthId})
        .WithMany(b => new {b.VersionId, b.Id})
        .HasForeignKey(p => p.VersionIdStrengthId);

    Property(p => p.Id).HasColumnName("iProductId");
    Property(p => p.VersionId).HasColumnName("iVersionID");
    Property(p => p.Name).HasColumnName("sName");
    Property(p => p.StrengthId).HasColumnName("iStrengthId");

}

UPDATE UPDATE

Or try this: 或尝试以下方法:

    HasRequired(p => new {p.VersionId, p.StrengthId})
        .WithMany()
        .HasForeignKey(b => new {b.VersionId, b.Id});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM