簡體   English   中英

實體框架6 TPT和外鍵

[英]Entity Framework 6 TPT and foreign keys

我一直試圖讓EF在TPT場景中與外鍵一起工作。 我認為一個表具有一對多的關系,而另一個表具有一對一的關系是很復雜的。

將此代碼簡化為說明問題(GitHub: https//github.com/Dash/EFTest for VS project)。

當我在基本Persons表中創建外鍵時,出現關於類型不匹配的錯誤:

產品:FromRole:NavigationProperty“產品”無效。 AssociationType'Product_Buyers'中FromRole'Product_Buyers_Target'的類型'Buyer'必須與聲明了該NavigationProperty的類型'Person'完全匹配。

為了解決這個問題,我將Product導航屬性移到了子級,但是由於數據庫中不存在ProductId列,我得到了一個錯誤-它試圖查找派生表而不是基表。

我尚不完全清楚如何解決此問題,並且一直繞着圈子兜兜轉轉,直到我的眼睛流血為止。 有人可以幫忙嗎?

模型

public abstract class Person
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual Product Product { get; set; }
}

public class Buyer : Person
{
    public string ShipTo { get; set; }
}

public class Seller : Person
{
    public int Rating { get; set; } = 0;
}

public class Product
{
    public int Id { get; set; }

    public string Description { get; set; }
    public virtual Seller Seller { get; set; }
    public virtual IList<Buyer> Buyers { get; set; }
}

架構

public class ProductSchema : EntityTypeConfiguration<Product>
{
    public ProductSchema()
    {
        HasKey(p => p.Id)
            .Property(p => p.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        Property(p => p.Description)
            .HasMaxLength(256);

        HasOptional(p => p.Seller)
            .WithRequired(i => i.Product)
            .Map(m => m.MapKey("ProductId"));

        HasMany(p => p.Buyers)
            .WithRequired(i => i.Product)
            .Map(m => m.MapKey("ProductId"));
    }
}

public class PersonSchema : EntityTypeConfiguration<Person>
{
    public PersonSchema()
    {
        ToTable("Persons");

        HasKey(p => p.Id)
            .Property(p => p.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        Property(p => p.Name)
            .HasMaxLength(128);
    }
}

public class SellerSchema : EntityTypeConfiguration<Seller>
{
    public SellerSchema()
    {
        ToTable("Sellers");

        HasKey(s => s.Id)
            .Property(s => s.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        Property(s => s.Rating);
    }
}

public class BuyerSchema : EntityTypeConfiguration<Buyer>
{
    public BuyerSchema()
    {
        ToTable("Buyers");

        HasKey(b => b.Id)
            .Property(b => b.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        Property(b => b.ShipTo)
            .HasMaxLength(256);
    }
}

SQL

CREATE TABLE Products (
Id INT IDENTITY PRIMARY KEY,
[Description] nvarchar(256),
)

create table Persons (
    Id int IDENTITY PRIMARY KEY,
    [Name] nvarchar(128),
    ProductId INT NOT NULL,
    FOREIGN KEY (ProductId) REFERENCES Products(Id)
)

CREATE TABLE Buyers (
    Id INT PRIMARY KEY,
    ShipTo nvarchar(256)
)

CREATE TABLE Sellers (
    Id INT PRIMARY KEY,
    Rating INT NOT NULL DEFAULT 0
)

DECLARE @ProductId int;
INSERT INTO Products ([Description]) VALUES ('Widget');
SET @ProductId = @@IDENTITY;

INSERT INTO Persons ([Name], ProductId) VALUES ('Bob the seller', @ProductId);
INSERT INTO Sellers (Id, Rating) VALUES (@@IDENTITY, 0);

INSERT INTO Persons ([Name], ProductId) VALUES ('Bert the buyer', @ProductId);
INSERT INTO Buyers (Id, ShipTo) VALUES (@@IDENTITY, 'Somewhere');

我從來沒有找到讓EF來處理這個概念的方法。

最后,我不得不放棄Persons的ProductId外鍵,並在EF打球之前在買賣雙方中都有重復的副​​本。 這有悖於規范化,但可行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM