【问题标题】:Identity Column Inserting Null With MVC Code First使用 MVC 代码首先插入 Null 的标识列
【发布时间】:2013-08-04 23:35:06
【问题描述】:

我正在使用 MVC 来生成我的数据库。我的 UserProfile 类中有以下代码结构:

[KeyAttribute()] 
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }

这可以很好地生成数据库,但是当我尝试插入一条记录时,它说 UserId 不能为空。我不知道为什么它不自动生成一个 Id 整数。

【问题讨论】:

    标签: sql asp.net-mvc ef-code-first


    【解决方案1】:

    这是因为我在更新数据库时遇到了一些问题。需要手动删除此表并重新创建它。现在工作正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      • 2023-03-28
      • 2011-12-13
      • 2014-05-28
      • 2021-08-21
      • 2015-09-02
      相关资源
      最近更新 更多