2

The question as a one liner

How do I get EF to generate as part of a code first migration

ALTER TABLE [DMS].[Forms] ADD  CONSTRAINT [DF_DMS.Forms_Id]  DEFAULT (newid()) FOR [Id]
GO

More detail of the problem

I'm to build out a simple table to store information about forms, as it happens I already have this working for another table in my db but if I show you the code (see below) you'll realise the issue I'm hitting is stranger than it might seem ...

First the working example entity:

[Table("Files", Schema = "DMS")]
public class File
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
    ...
}

ok, now the not working one ...

[Table("Forms", Schema = "CMS")]
public class Form
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
    ...
}

As stated in the question, I'm using EF code first, so here's the migration code:

The working one:

CreateTable(
    "DMS.Files",
    c => new
        {
            Id = c.Guid(nullable: false),
            ...
        })
    .PrimaryKey(t => t.Id)
    ...;

... and the non working one ...

CreateTable(
    "CMS.Forms",
    c => new
        {
            Id = c.Guid(nullable: false),
            ...
        })
    .PrimaryKey(t => t.Id)
    ...;

So all is good ... then I migrate my db with an "update-database" command and script the two tables ...

The working one:

...
CREATE TABLE [DMS].[Files](
    [Id] [uniqueidentifier] NOT NULL,
    ...
 CONSTRAINT [PK_DMS.Files] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

ALTER TABLE [DMS].[Files] ADD  CONSTRAINT [DF_DMS.Files_Id]  DEFAULT (newid()) FOR [Id]
GO
...

... and the non working one ...

...
CREATE TABLE [CMS].[Forms](
    [Id] [uniqueidentifier] NOT NULL,
    ...
 CONSTRAINT [PK_CMS.Forms] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO
...
Connell.O'Donnell
  • 3,603
  • 11
  • 27
  • 61
War
  • 8,539
  • 4
  • 46
  • 98
  • 1
    Do you have any other config going on in the `OnModelCreating` method perhaps? – DavidG Jun 19 '18 at 14:28
  • Can you post the `constructor` of `File`? ... which by second thoughts is irrelevant since your sql differs. – Stefan Jun 19 '18 at 14:29
  • @DavidG this is all the code (except where I snipped with ...) relating to the entities in question. The bits I snipped refer to foreign keys and other properties of the entities, i cut them as they didn't seem relevant to the question. I can gist the full code for the classes here if it helps though. – War Jun 19 '18 at 15:05
  • both files have no ctor @Stefan ... but yeh you're right in this case that should make no difference, it's only the SQL i'm looking to fix here ... I could manually add a line to the generated migration but I want to understand where this difference is coming from. – War Jun 19 '18 at 15:06
  • Is the second table already created and you added the attribute already? like this: https://github.com/aspnet/EntityFrameworkCore/issues/1611 ? – L0uis Jun 19 '18 at 15:08
  • initially yeh ... then because i had both migrations locally i removed them back to when the db didn't have the faulty table then generated a single new migration, this is how I ended up with like for like scripts in the migrations. – War Jun 19 '18 at 15:23

1 Answers1

1

I have no idea why but this works and fixes the problem ...

The working one (no changes):

CreateTable(
    "DMS.Files",
    c => new
        {
            Id = c.Guid(nullable: false),
            ...
        })
    .PrimaryKey(t => t.Id)
    ...;

... and the non working one (with fix applied)...

CreateTable(
    "CMS.Forms",
    c => new
        {
            Id = c.Guid(nullable: false, defaultValueSql: "newid()"),
            ...
        })
    .PrimaryKey(t => t.Id)
    ...;

I figure I must have come across some sort of bug in EF that results in it generating the migration correctly in some cases.

War
  • 8,539
  • 4
  • 46
  • 98