EF6数据库首先在外键上出现每表类型多重性问题

时间:2017-08-22 19:40:59

标签: c# entity-framework-6 ef-database-first

我对EF很新,所以我希望这不是一个愚蠢的问题。我搜索过,但找不到与我的问题相关的任何内容。

我在使用EF6建模现有数据库时遇到了问题。我有TPT继承设置如下:

TPT model 由于创建模型的问题,我必须手动添加BAMS * Environment表。在将BAMSPortalEnvironment中的外键添加到BAMSPrimeEnvironment之前,一切都很好。对于给定的Prime环境,可能有也可能没有Portal环境,但是对于每个Portal环境,都有Prime环境 我从VS2017收到以下错误:

  

运行转换:多重性在角色' BAMSPortalEnvironment'中无效。在关系&FK_BAMSPortalEnvironment_BAMSPrimeEnvironment'。由于Dependent Role属性不是关键属性,因此Dependent Role的多重性的上限必须为*。

相关表格的简化模型:

CREATE TABLE dbo.Environment(
  EnvironmentId     int IDENTITY(1,1) NOT NULL,
  EnvironmentTypeId char(1) NOT NULL,
  [Description]     varchar(64) NOT NULL,
  CONSTRAINT UQ_Environment UNIQUE NONCLUSTERED (EnvironmentId ASC) ON [PRIMARY],
  CONSTRAINT PK_Environment PRIMARY KEY CLUSTERED (EnvironmentId ASC, EnvironmentTypeId ASC) ON [PRIMARY]
) ON [PRIMARY];

CREATE TABLE dbo.BAMSPrimeEnvironment(
    EnvironmentId       int NOT NULL,
    EnvironmentTypeId   AS CAST('B' AS char(1)) PERSISTED NOT NULL,
    CONSTRAINT FK_BAMSPrimeEnvironment_Environment FOREIGN KEY (EnvironmentId, EnvironmentTypeId) REFERENCES dbo.Environment (EnvironmentId, EnvironmentTypeId) ON UPDATE NO ACTION ON DELETE CASCADE,
    CONSTRAINT PK_BAMSPrimeEnvironment PRIMARY KEY CLUSTERED (EnvironmentId ASC, EnvironmentTypeId ASC) ON [PRIMARY]
) ON [PRIMARY];

CREATE TABLE dbo.BAMSPortalEnvironment(
    EnvironmentId       int NOT NULL,
    EnvironmentTypeId   AS CAST('P' AS char(1)) PERSISTED NOT NULL,
    BAMSPrimeId         int NOT NULL,
    BAMSPrimeTypeId     AS CAST('B' AS char(1)) PERSISTED NOT NULL,
    CONSTRAINT FK_BAMSPortalEnvironment_BAMSPrimeEnvironment FOREIGN KEY (BAMSPrimeId, BAMSPrimeTypeId) REFERENCES dbo.BAMSPrimeEnvironment (EnvironmentId, EnvironmentTypeId) ON UPDATE NO ACTION ON DELETE NO ACTION,
    CONSTRAINT FK_BAMSPortalEnvironment_Environment FOREIGN KEY (EnvironmentId, EnvironmentTypeId) REFERENCES dbo.Environment (EnvironmentId, EnvironmentTypeId) ON UPDATE NO ACTION ON DELETE CASCADE,
    CONSTRAINT UQ_BAMSPortalEnvironment_BAMSPrimeId_BAMSPrimeTypeId UNIQUE NONCLUSTERED (BAMSPrimeId ASC, BAMSPrimeTypeId ASC) ON [PRIMARY],
    CONSTRAINT PK_BAMSPortalEnvironment PRIMARY KEY CLUSTERED (EnvironmentId ASC, EnvironmentTypeId ASC) ON [PRIMARY]
) ON [PRIMARY];

我可以"修复"通过改变" 0..1"到" *",但这并不能准确描述我的模型。我错过了什么让这个工作?

这是我的第一篇文章,如果您需要其他信息,请与我们联系。

谢谢!

1 个答案:

答案 0 :(得分:0)

我无法找到解决方案,因此我改为引用BAMSPrimeEnvironment而不是BAMSPortalEnvironment,而是将其更改为引用环境,这解决了这个问题。