Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert fails when model has tables with same name in different schemas #30087

Closed
Alexander-Leontiev opened this issue Jan 18, 2023 · 6 comments
Closed

Comments

@Alexander-Leontiev
Copy link

I'm in progress of updating EF Core to version 7 from 3.
In my DB model I have some tables, which have same names but different schemas. My current EF Core 3.1.15 copes well with it.
However EF Core 7.0.1 fails to handle it. If I try to save new entities for different schemas at once, EF generates wrong queries with negative values for FK columns.

My model looks like the following:

[Table("User", Schema = "dbo")]
public class User {
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    
    public virtual UserSettings Settings { get; set; }
    public virtual UserSettingsAnotherSchema AnotherSettings { get; set; }
} 

[Table("UserSettings", Schema = "dbo")]
public class UserSettings {
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int UserId { get; set; }

    public string SomeColumn { get; set; }

    public virtual User User { get; set; }
}

[Table("UserSettings", Schema = "another")]
public class UserSettingsAnotherSchema {
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int UserId { get; set; }
    public string AnotherColumn { get; set; }
        
    public virtual User User { get; set; }
}

When I try to save new User with Settings properties been initialized, EF generates queries like:

exec sp_executesql N'SET NOCOUNT ON;
INSERT INTO [dbo].[User] ([Name])
OUTPUT INSERTED.[Id]
VALUES (@p0);
INSERT INTO [dbo].[UserSettings] ([UserId], [SomeColumn])
VALUES (@p1, @p2);
',N'@p0 nvarchar(4000),@p1 int,@p2 nvarchar(4000)',@p0=N'test2',@p1=-2147482647,@p2=N'test2'

exec sp_executesql N'SET NOCOUNT ON;
MERGE [dbo].[User] USING (
VALUES (@p0, 0),
(@p1, 1)) AS i ([Name], _Position) ON 1=0
WHEN NOT MATCHED THEN
INSERT ([Name])
VALUES (i.[Name])
OUTPUT INSERTED.[Id], i._Position;
INSERT INTO [dbo].[UserSettings] ([UserId], [SomeColumn])
VALUES (@p2, @p3),
(@p4, @p5);
',N'@p0 nvarchar(4000),@p1 nvarchar(4000),@p2 int,@p3 nvarchar(4000),@p4 int,@p5 nvarchar(4000)',@p0=N'test2',@p1=N'test1',@p2=-2147482647,@p3=N'test2',@p4=-2147482646,@p5=N'test1'

As you can see, instead of using generated PK value from parent table EF tries to insert some synthetic negative values into FK columns of child tables.

Project to reproduce the issue: EfTest.zip

EF Core version: 7.0.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: net6.0

@roji
Copy link
Member

roji commented Jan 18, 2023

@Alexander-Leontiev can you please try the latest 8.0 daily build? There have been several fixes around this area for 7.0.2 and 7.0.3, testing the daily build would tell us whether your issue is a dup of one of those.

@Alexander-Leontiev
Copy link
Author

@roji 7.0.2 still has the issue. 7.0.3 failed to be restored.
However, 8.0.0-alpha.1.23067.1 fixes my issue. I hope it's possible to backport it to some 7.* release.

@roji
Copy link
Member

roji commented Jan 18, 2023

If 7.0.0 fixed the issue, then 7.0.3 already contained the fix - all the fixes in this area have already been backported.

@Alexander-Leontiev
Copy link
Author

@roji sounds great! When 7.0.3 public release is expected?

@roji
Copy link
Member

roji commented Jan 18, 2023

Not long - in mid-February.

@ajcvickers
Copy link
Member

ajcvickers commented Jan 19, 2023

Duplicate of #29741 😉

@ajcvickers ajcvickers marked this as a duplicate of #29741 Jan 19, 2023
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Jan 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants