简体   繁体   中英

EF6 Database First Table-per-Type multiplicity issue on foreign key

I am fairly new to EF, so I hope this isn't a stupid question. I searched, but couldn't find anything relative to my issue.

I've been having issues modeling an existing database in EF6. I have TPT inheritance setup as follows:

TPT model I have to manually add the BAMS*Environment tables due to issues creating the model. Everything is fine until I add the Foreign key from BAMSPortalEnvironment to BAMSPrimeEnvironment. For a given Prime environment, there may or may not be a Portal environment, but for every Portal environment, there is a Prime environment.
I get the following error from VS2017:

Running transformation: Multiplicity is not valid in Role 'BAMSPortalEnvironment' in relationship 'FK_BAMSPortalEnvironment_BAMSPrimeEnvironment'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be *.

Simplified model of the relevant tables:

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];

I can "fix" this by changing the "0..1" to "*", but that does not accurately describe my model. What am I missing to get this working?

This is my first post, so if you need additional information, let me know.

Thanks!

我找不到解决方案,因此我没有将BAMSPortalEnvironment引用BAMSPrimeEnvironment,而是将其更改为引用Environment,从而解决了此问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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