0

我在一些项目EF代码中首先使用TPH(在部分类中)我有这样的类:

namespace Billing.Model.Domain.Entities.BilProject
{

    [Table("ProjectPriceList")]
    public abstract class ProjectPriceList
    {
        [Key, Column("ProjectId", Order = 0)]
        public int ProjectId { get; set; }
        [Key, Column("PriceListId", Order = 1)] 
        public int PriceListId { get; set; }
        [Key, Column("PriceListSourceType", Order = 2)]
        public int PriceListSourceType { get; set; }
        [ForeignKey("PriceListSourceType")]
        public virtual BillingProjectCode PriceListSourceTypeCode { get; set; }
        public int AddUsrnm { get; set; }
        public DateTime AddTmstmp { get; set; }

        [ForeignKey("ProjectId")]
        public virtual BillingProject BillingProject { get; set; }
        public virtual SnapShotPriceList SnapShotPriceList{ get; set; }
    }

    public class BillingProjectPriceList : ProjectPriceList
    {
        [ForeignKey("PriceListId")]
        public virtual PriceList PriceList { get; set; }
    }
    public class EstimateProjectPriceList : ProjectPriceList
    {
    }
}

当我尝试从控制台通过 Update-Database -Script 生成 sql 代码以创建数据库时,我收到此 SQL 作为结果:

CREATE TABLE [dbo].[ProjectPriceList] (
    [ProjectId] [int] NOT NULL,
    [PriceListId] [int] NOT NULL,
    [PriceListSourceType] [int] NOT NULL,
    [AddUsrnm] [int] NOT NULL,
    [AddTmstmp] [datetime] NOT NULL,
    [Discriminator] [nvarchar](128) NOT NULL,
    CONSTRAINT [PK_dbo.ProjectPriceList] PRIMARY KEY ([ProjectId], [PriceListId], [PriceListSourceType])
)

CREATE INDEX [IX_ProjectId] ON [dbo].[ProjectPriceList]([ProjectId])
CREATE INDEX [IX_PriceListId] ON [dbo].[ProjectPriceList]([PriceListId])
CREATE INDEX [IX_PriceListSourceType] ON [dbo].[ProjectPriceList]([PriceListSourceType])

ALTER TABLE [dbo].[ProjectPriceList] ADD CONSTRAINT [FK_dbo.ProjectPriceList_dbo.BillingProject_ProjectId] FOREIGN KEY ([ProjectId]) REFERENCES [dbo].[BillingProject] ([ProjectId]) ON DELETE CASCADE
ALTER TABLE [dbo].[ProjectPriceList] ADD CONSTRAINT [FK_dbo.ProjectPriceList_dbo.BillingProjectCode_PriceListSourceType] FOREIGN KEY ([PriceListSourceType]) REFERENCES [dbo].[BillingProjectCode] ([CodeId]) ON DELETE CASCADE
ALTER TABLE [dbo].[ProjectPriceList] ADD CONSTRAINT [FK_dbo.ProjectPriceList_dbo.PriceList_PriceListId] FOREIGN KEY ([PriceListId]) REFERENCES [dbo].[PriceList] ([PriceListId]) ON DELETE CASCADE

最后一个 FK 约束是多余的,如果我尝试添加新的 EstimateProjectPriceList 会给我带来麻烦。我的问题是如何通过 OnModelCreating 中的一些代码 Data Annotations 或 Fluent Api 停止创建这个 FK ?

谢谢任何帮助..

4

0 回答 0