【问题标题】:[DatabaseGenerated(DatabaseGeneratedOption.Identity)][DatabaseGenerated(DatabaseGeneratedOption.Identity)]
【发布时间】:2018-03-25 02:44:11
【问题描述】:

我现在可能已经花了 4 个小时尝试了 代码优先方法和 数据库优先方法

 db.MoveJobs.Add(moveJob);
 db.SaveChanges();

当插入一行时,它会给我

无法将值 NULL 插入到列“id”、表“aspnet-AccountVerification.Web3.dbo.MoveJobs”中;列不允许空值。插入失败。 声明已终止。

public class MoveJob
{
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string Name { get; set; }
        public string CountryCode { get; set; }
        public string PhoneNumber { get; set; }
}

基本上,我需要为每个新行生成增量键。没有堆栈溢出/论坛/或有什么帮助。

不过我昨天工作很奇怪。我不知道哪里出错了

【问题讨论】:

  • 这只是意味着您的Id 列不是数据库中的标识列。
  • 您似乎正在为列 ID 发送 null ..您可以检查您发送的数据
  • 我已将 Id 列更改为身份列。 [id] INT NOT NULL IDENTITY,我通过服务器资源管理器使用本地 mdf 文件。我怎样才能使更改永久/保存?
  • 你不需要使用[DatabaseGenerated(DatabaseGeneratedOption.Identity)]。默认情况下,id 属性是密钥和标识。首先,删除此属性并删除您的数据库,然后添加新的迁移。你使用流畅的 API 吗?

标签: c# sql-server entity-framework


【解决方案1】:

你不需要使用[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

属性id默认是key和identity。首先删除此属性并删除您的数据库,然后添加新的迁移。

【讨论】:

    猜你喜欢
    • 2020-06-25
    • 1970-01-01
    • 2012-06-14
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 2015-10-26
    • 1970-01-01
    相关资源
    最近更新 更多