EF代码优先迁移,DB生成了GUID密钥 [英] EF code first migrations, DB generated guid keys

查看:109
本文介绍了EF代码优先迁移,DB生成了GUID密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个问题

如何让EF作为代码首次迁移的一部分生成

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

问题的更多详细信息

我要构建一个简单的表来存储有关表单的信息,因为我已经在数据库中为另一个表工作了,但是如果我向您展示代码(见下文),您将意识到这个问题. '击中比看起来更奇怪...

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 more strange than it might seem ...

首先是工作示例实体:

[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; }
    ...
}

如问题中所述,我首先使用EF代码,所以这是迁移代码:

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

正在工作的人:

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)
    ...;

所以一切都很好...然后我使用"update-database"命令迁移数据库,并为两个表编写脚本...

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

正在工作的人:

...
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
...

推荐答案

我不知道为什么,但这可以解决问题...

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)
    ...;

我认为我肯定在EF中遇到过某种错误,导致在某些情况下它可以正确生成迁移.

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

这篇关于EF代码优先迁移,DB生成了GUID密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆