Closed seantarogers closed 2 years ago
Related to #9696
Any work around until its fixed?
I had to move my collection to the base type and expose it in the inherited type.
@AndriySvyryd @bricelam This model now works with the current bits, except that two identical but differently named foreign key constraints are created:
CREATE TABLE [Session] (
[Id] int NOT NULL IDENTITY,
[Discriminator] nvarchar(max) NOT NULL,
[BackOfficeName] nvarchar(max) NULL,
[QuotingName] nvarchar(max) NULL,
CONSTRAINT [PK_Session] PRIMARY KEY ([Id])
);
CREATE TABLE [Policy] (
[Id] int NOT NULL IDENTITY,
[SessionId] int NOT NULL,
[Discriminator] nvarchar(max) NOT NULL,
[BackOfficePolicyName] nvarchar(max) NULL,
[QuotingPolicyName] nvarchar(max) NULL,
CONSTRAINT [PK_Policy] PRIMARY KEY ([Id]),
CONSTRAINT [FK_Policy_Session_SessionId] FOREIGN KEY ([SessionId]) REFERENCES [Session] ([Id]) ON DELETE NO ACTION,
CONSTRAINT [FK_Policy_Session_SessionId1] FOREIGN KEY ([SessionId]) REFERENCES [Session] ([Id]) ON DELETE NO ACTION
);
Is there some reason that the constraint isn't collapsed into one in this case? (It creates issues having two constraints when cascade delete is setup on both.)
Putting this on the backlog to consolidate the two constraints together, if it can be done safely. For now, the workaround is to explicitly give both constraints the same name. For example:
public static void ConfigureBackOfficeSession(ModelBuilder modelBuilder)
{
var entity = modelBuilder.Entity<BackOfficeSession>();
entity.Property(x => x.BackOfficeName);
entity.HasMany(c => c.Policies).WithOne().HasForeignKey(c => c.SessionId).HasConstraintName("MyConstraint");
}
public static void ConfigureQuotingSession(ModelBuilder modelBuilder)
{
var entity = modelBuilder.Entity<QuotingSession>();
entity.Property(x => x.QuotingName);
entity.HasMany(c => c.Policies).WithOne().HasForeignKey(c => c.SessionId).HasConstraintName("MyConstraint");
}
Is the issue still not fixed? I'm also trying to use TPH pointing to another TPH, fails with the same strange exception.
Closing in favor of #12963
Issue
I am trying to use Table Per Hierarchy inheritance in conjunction with a one to many relationship. Both the Parent and child entities use inheritance. I have a very simple entity model. I have a one base parent entity: Session which has two entities which extend from it: QuotingSession and BackOfficeSession. Both of these two parent entities contain a collection of child entities (a one to many relationship). The child entities are also built using inheritance. There is a base child entity: Policy. This base child entity is extended by two entities: QuotingPolicy and BackOfficePolicy.
When I construct either of the Parent entities and attempt to save I receive this exception:
Full stack trace:
Steps to reproduce
Reproduction projects
Here is a reproduction of the problem in both EF Core 1.1.1 and EF Core 2.0.1, plus a project that successfully saves the same structure in EF 6:
https://github.com/seantarogers/EFTest
Database structure I am trying to write to
Class structure:
1. Session
2. QuotingSession
3. BackOfficeSession
4. Policy
5. QuotingPolicy
6. BackOfficePolicy
7. EF DB Context and Fluent Configuration
8. To test it
Further technical details
EF Core version: 1.1.1 and 2.0.1 Database Provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) Operating system: Windows 7 and Windows 10 IDE: Visual Studio 2017 Enterprise 15.3.0
Thanks