c# - Entity Framework 6 Guid 生成空?

标签 c# asp.net sql-server database entity-framework

我正在使用 Asp.net mvc 5 和 EF 6 制作网络应用程序。我从 Internet 应用程序模板开始并添加了这些类:

public class Article
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid ID { get; set; }
    public string Tags { get; set; } 
    public string Name { get; set; }
    public string Link { get; set; }
    public string HtmlContent { get; set; }
    [ForeignKey("ListaId")]
    public Lista Lista { get; set; }
    public Guid ListaId { get; set; }
}

public class List
{

    public string Name { get; set; }
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid ID { get; set; }
    public int Status { get; set; } 
    public virtual ApplicationUser User { get; set; }
    public string ApplicationUserId { get; set; }
    public string Tags { get; set; } 
}

但每次 nuget 包管理器更新数据库并运行 Seed 方法时,它都会在尝试向数据库中插入文章时中断。 这是 configuration.cs 中的种子方法

        context.Lists.AddOrUpdate(new Lista
        {
            Name = "Technology",
            ApplicationUserId = "f06b0d2e-2088-4cd7-8b1c-4fcad5619f3c",
            Status = 1,
            Tags = "Tech,News"
        });
        context.SaveChanges();

        Lista l1 = context.Lists.Where(x => x.Status == 1).First();

        context.Articles.AddOrUpdate(new Article
        {
            ListaId = l1.ID,
            Link = "www.abc.com",
            Name = "ABC",
            Tags = "c,test",
            HtmlContent = "Hello World"
        });
        context.SaveChanges(); //here it breaks

The inner exception: System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'ID', table 'aspnet-Webapp-20141022011020.dbo.Articles'; column does not allow nulls. INSERT fails I get the same error if I take the [Key] adnotation out of the Article class and if I take the [DatabaseGenerated(DatabaseGeneratedOption.Identity)] adnotation I get the following error: System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK_dbo.Articles'. Cannot insert duplicate key in object 'dbo.Articles'. The duplicate key value is (00000000-0000-0000-0000-000000000000).

在 Guid 的位置使用 long/int,我得到一个更新数据库错误,例如:uniqueidentifier is incompatible with bigint/int。

我能做什么?为什么它不使用 guid 生成正确的 ID?

最佳答案

你必须告诉数据库你想要这样的行为。通常,如果您使用代码优先方法,EF6 就足够智能,它将能够生成正确的行为。

进入数据库,修改ID字段,并将其添加为默认值:newsequentialid()

如果您想自己创建 Guid:

public class Article
{
    [Key]
    public Guid ID { get; set; }
    public string Tags { get; set; } 
    public string Name { get; set; }
    public string Link { get; set; }
    public string HtmlContent { get; set; }
    [ForeignKey("ListaId")]
    public Lista Lista { get; set; }
    public Guid ListaId { get; set; }

    public Article(){
       ID = Guid.NewGuid();
    }

}

关于c# - Entity Framework 6 Guid 生成空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27680502/

相关文章:

c# - 如何检查一个字符串是否不等于所有这些?

c# - 如何取消绑定(bind)ObjectDataSource?

jquery - 单击表头时对表进行排序

c# - datetimeoffset.datetime 问题?

sql - 如何在 SQL-Server 中将日期转换为 ISO 8601?

PHP/SQL - 如何更新数据库字段

c# - 覆盖和隐藏之间的确切区别

c# - 如何将 Metro UI 控件添加到 visual studio 中的工具箱?

c#异常进程无法访问文件

asp.net - 浏览器不会在支付网关向我们网站发出的发布请求上设置 ASP.NET_SessionId cookie