dotnet / dotnet/efcore

Owned table using entity splitting and mapped to different table doesn't work in SqlServer

Open
#29,075 4 comments 0 reactions 0 assignees View on GitHub
area-model-building area-sqlserver
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

```C#
mb.Entity(
b =>
{
b.ToTable("EntityOnes");

b.OwnsOne(e => e.OwnedReference,
o =>
{
o.ToTable("OwnedReferences");

o.SplitToTable("OwnedReferenceExtras1",
t =>
{
t.Property(e => e.OwnedIntValue3);
t.Property(e => e.OwnedStringValue3);
});

o.SplitToTable("OwnedReferenceExtras2",
t =>
{
t.Property(e => e.OwnedIntValue4);
t.Property(e => e.OwnedStringValue4);
});
});
});

public class EntityOne
{
public int Id { get; set; }
public string StringValue1 { get; set; }
public string StringValue2 { get; set; }
public string StringValue3 { get; set; }
public string StringValue4 { get; set; }
public int IntValue1 { get; set; }
public int IntValue2 { get; set; }
public int IntValue3 { get; set; }
public int IntValue4 { get; set; }
public OwnedReference OwnedReference { get; set; }
}

public class OwnedReference
{
public int Id { get; set; }
public string OwnedStringValue1 { get; set; }
public string OwnedStringValue2 { get; set; }
public string OwnedStringValue3 { get; set; }
public string OwnedStringValue4 { get; set; }
public int OwnedIntValue1 { get; set; }
public int OwnedIntValue2 { get; set; }
public int OwnedIntValue3 { get; set; }
public int OwnedIntValue4 { get; set; }
}

```

Issue:
Tables for additional fragments
```
CREATE TABLE [OwnedReferenceExtras1] (
[EntityOneId] int NOT NULL,
[OwnedStringValue3] nvarchar(max) NULL,
[OwnedIntValue3] int NOT NULL,
CONSTRAINT [PK_OwnedReferenceExtras1] PRIMARY KEY ([EntityOneId]),
CONSTRAINT [FK_OwnedReferenceExtras1_EntityOnes_EntityOneId] FOREIGN KEY ([EntityOneId]) REFERENCES [EntityOnes] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_OwnedReferenceExtras1_OwnedReferences_EntityOneId] FOREIGN KEY ([EntityOneId]) REFERENCES [OwnedReferences] ([EntityOneId]) ON DELETE CASCADE
)
```
Microsoft.Data.SqlClient.SqlException : Introducing FOREIGN KEY constraint 'FK_OwnedReferenceExtras1_OwnedReferences_EntityOneId' on table 'OwnedReferenceExtras1' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.