efcore icon indicating copy to clipboard operation
efcore copied to clipboard

T-SQL migration output has FK referenced_table_name and ref_column in incorrect order

Open swaggerloaf opened this issue 11 years ago • 5 comments

runtime: 1.0.0-beta4-11158 clr x86 ef version: 7.0.0-beta4-12405

On apply migration, T-SQL sent to database has foreign key scripted as:

CONSTRAINT [FK_AspNetRoleClaims_AspNetRoles_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [Id].[AspNetRoles]

with referenced_table_name and ref_column in reverse order

syntax should be REFERENCES [ schema_name . ] referenced_table_name [ ( ref_column ) ]

Context is

    public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext()
        {
        }

        protected override void OnConfiguring(DbContextOptions options)
        {
           // options.UseInMemoryStore(true);
            options.UseSqlServer("Server=Glenn-PC\\SQLEXPRESS;Database=NeoSample;Trusted_Connection=True;MultipleActiveResultSets=true");
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
        }
    }

Full T-SQL

CREATE TABLE [AspNetRoles] (
    [ConcurrencyStamp] nvarchar(max),
    [Id] nvarchar(450),
    [Name] nvarchar(max),
    [NormalizedName] nvarchar(max),
    CONSTRAINT [PK_AspNetRoles] PRIMARY KEY NONCLUSTERED ([Id])
);
CREATE TABLE [AspNetUserRoles] (
    [RoleId] nvarchar(450),
    [UserId] nvarchar(450),
    CONSTRAINT [PK_AspNetUserRoles] PRIMARY KEY NONCLUSTERED ([UserId], [RoleId])
);
CREATE TABLE [AspNetUsers] (
    [AccessFailedCount] int NOT NULL,
    [ConcurrencyStamp] nvarchar(max),
    [Email] nvarchar(max),
    [EmailConfirmed] bit NOT NULL,
    [Id] nvarchar(450),
    [LockoutEnabled] bit NOT NULL,
    [LockoutEnd] datetimeoffset,
    [NormalizedEmail] nvarchar(max),
    [NormalizedUserName] nvarchar(max),
    [PasswordHash] nvarchar(max),
    [PhoneNumber] nvarchar(max),
    [PhoneNumberConfirmed] bit NOT NULL,
    [SecurityStamp] nvarchar(max),
    [TwoFactorEnabled] bit NOT NULL,
    [UserName] nvarchar(max),
    CONSTRAINT [PK_AspNetUsers] PRIMARY KEY NONCLUSTERED ([Id])
);
CREATE TABLE [AspNetRoleClaims] (
    [ClaimType] nvarchar(max),
    [ClaimValue] nvarchar(max),
    [Id] int NOT NULL IDENTITY,
    [RoleId] nvarchar(450),
    CONSTRAINT [PK_AspNetRoleClaims] PRIMARY KEY NONCLUSTERED ([Id]),
    CONSTRAINT [FK_AspNetRoleClaims_AspNetRoles_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [Id].[AspNetRoles]
);
CREATE TABLE [AspNetUserClaims] (
    [ClaimType] nvarchar(max),
    [ClaimValue] nvarchar(max),
    [Id] int NOT NULL IDENTITY,
    [UserId] nvarchar(450),
    CONSTRAINT [PK_AspNetUserClaims] PRIMARY KEY NONCLUSTERED ([Id]),
    CONSTRAINT [FK_AspNetUserClaims_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [Id].[AspNetUsers]
);
CREATE TABLE [AspNetUserLogins] (
    [LoginProvider] nvarchar(450),
    [ProviderDisplayName] nvarchar(max),
    [ProviderKey] nvarchar(450),
    [UserId] nvarchar(450),
    CONSTRAINT [PK_AspNetUserLogins] PRIMARY KEY NONCLUSTERED ([LoginProvider], [ProviderKey]),
    CONSTRAINT [FK_AspNetUserLogins_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [Id].[AspNetUsers]
);
CREATE TABLE [dbo].[__MigrationHistory] (
    [MigrationId] nvarchar(150) NOT NULL,
    [ContextKey] nvarchar(300) NOT NULL,
    [ProductVersion] nvarchar(32) NOT NULL,
    CONSTRAINT [PK_MigrationHistory] PRIMARY KEY ([MigrationId], [ContextKey])
)
INSERT INTO [dbo].[__MigrationHistory] ([MigrationId], [ContextKey], [ProductVersion])
VALUES ('20150228142559_CreateIdentitySchema', 'NeoSample.DbContexts.ApplicationDbContext', '7.0.0-beta4-12405')

swaggerloaf avatar Feb 28 '15 15:02 swaggerloaf

Isn't this another dupe of #1612?

bricelam avatar Feb 28 '15 21:02 bricelam

The scaffolded call to AddForeignKey is wrong. It passes the column as the schema.

bricelam avatar Feb 28 '15 21:02 bricelam

#1612 had a () at end

swaggerloaf avatar Mar 01 '15 00:03 swaggerloaf

Maybe need to inject default schema dbo as in

REFERENCES [dbo].[AspNetRoles] ([Id])

swaggerloaf avatar Mar 01 '15 16:03 swaggerloaf

Dupe of #1612 BTW you can work around this by changing the scaffolded code to use correct overload of ForeignKey, example change here https://github.com/rowanmiller/UnicornStore/commit/b71cfc1ff155d0d580e4ca7fd70c4d019982cef3#diff-ead41050d624017563d55a02d52ab1e1R56

rowanmiller avatar Mar 04 '15 19:03 rowanmiller