EntityFramework 6(代码优先)SQL 数据库标识交互问题 - EF 尝试在标识列中插入显式值
Issue with EntityFramework 6 (Code First) SQL DB Identity interaction - EF tries to insert explicit value in identity column
我有一个 SQL 数据库,有一个 table 由几列组成,有一个 'user_id' 列,设置为 PK 和身份。
这是 table 的代码:
CREATE TABLE [dbo].[Users] (
[user_id] INT IDENTITY (1, 1) NOT NULL,
[firstname] NVARCHAR (25) NOT NULL,
[lastname] NVARCHAR (25) NOT NULL,
[title] NVARCHAR (10) NOT NULL,
[email] NVARCHAR (50) NOT NULL,
[password] NVARCHAR (255) NOT NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED ([user_id] ASC)
);
尝试在数据库中保存新对象时,会产生错误
SqlException: Cannot insert explicit value for identity column in table
'Users' when IDENTITY_INSERT is set to OFF.
正在保存的对象没有被赋予'user_id'。
'user_id' 的 DBContext 是:
entity.Property(e => e.UserId).HasColumnName("user_id");
并且由 EFCorePowerTools 自动生成。
实际用户 class 有 属性 'UserID',绑定到 DBContext 中的 'user_id'。
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity), Key]
public int UserId { get; set; }
想法是让 SQL 数据库处理密钥生成。
通过查询手动插入效果很好,所以问题一定出在 EF 实现上。
到目前为止,我已经尝试过:
使 'User' 中的 'UserID' 可为空,
只有'[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]作为'UserID'
的注解
重新制作数据库,然后重新制作 DBContext
出于某种原因,即使 'User' 中的 'UserID' 被注释为 [NotMapped],我也会遇到同样的错误,这让我相信实际的 DBContext 绑定中存在问题。
这是 'Users' 的完整 DBContext:
modelBuilder.Entity<User>(entity =>
{
entity.Property(e => e.UserId).HasColumnName("user_id");
entity.Property(e => e.Email)
.IsRequired()
.HasColumnName("email")
.HasMaxLength(50);
entity.Property(e => e.Firstname)
.IsRequired()
.HasColumnName("firstname")
.HasMaxLength(25);
entity.Property(e => e.Lastname)
.IsRequired()
.HasColumnName("lastname")
.HasMaxLength(25);
entity.Property(e => e.Password)
.IsRequired()
.HasColumnName("password")
.HasMaxLength(255);
entity.Property(e => e.Title)
.IsRequired()
.HasColumnName("title")
.HasMaxLength(10);
});
以及所有 'User' class:
public partial class User : DomainAppBase, IStorable
{
public User()
{
Leads = new HashSet<Lead>();
}
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Title { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public override void SetDefaultValues()
{
UserId = NullKey;
}
[NotMapped]
public ICollection<Lead> Leads { get; set; }
[NotMapped]
public new int Key => UserId;
}
任何帮助或指点将不胜感激。提前致谢。
请将模型构建器更新为
modelBuilder.Entity<User>(entity =>
{
entity.HasKey(o => o.UserId );
entity.Property(e => e.UserId).ValueGeneratedOnAdd().HasColumnName("user_id")
...................
ValueGeneratedOnAdd
允许在添加新实体时生成一个值。
我有一个 SQL 数据库,有一个 table 由几列组成,有一个 'user_id' 列,设置为 PK 和身份。
这是 table 的代码:
CREATE TABLE [dbo].[Users] (
[user_id] INT IDENTITY (1, 1) NOT NULL,
[firstname] NVARCHAR (25) NOT NULL,
[lastname] NVARCHAR (25) NOT NULL,
[title] NVARCHAR (10) NOT NULL,
[email] NVARCHAR (50) NOT NULL,
[password] NVARCHAR (255) NOT NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED ([user_id] ASC)
);
尝试在数据库中保存新对象时,会产生错误
SqlException: Cannot insert explicit value for identity column in table
'Users' when IDENTITY_INSERT is set to OFF.
正在保存的对象没有被赋予'user_id'。
'user_id' 的 DBContext 是:
entity.Property(e => e.UserId).HasColumnName("user_id");
并且由 EFCorePowerTools 自动生成。
实际用户 class 有 属性 'UserID',绑定到 DBContext 中的 'user_id'。
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity), Key]
public int UserId { get; set; }
想法是让 SQL 数据库处理密钥生成。
通过查询手动插入效果很好,所以问题一定出在 EF 实现上。
到目前为止,我已经尝试过:
使 'User' 中的 'UserID' 可为空,
只有'[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]作为'UserID'
的注解重新制作数据库,然后重新制作 DBContext
出于某种原因,即使 'User' 中的 'UserID' 被注释为 [NotMapped],我也会遇到同样的错误,这让我相信实际的 DBContext 绑定中存在问题。
这是 'Users' 的完整 DBContext:
modelBuilder.Entity<User>(entity =>
{
entity.Property(e => e.UserId).HasColumnName("user_id");
entity.Property(e => e.Email)
.IsRequired()
.HasColumnName("email")
.HasMaxLength(50);
entity.Property(e => e.Firstname)
.IsRequired()
.HasColumnName("firstname")
.HasMaxLength(25);
entity.Property(e => e.Lastname)
.IsRequired()
.HasColumnName("lastname")
.HasMaxLength(25);
entity.Property(e => e.Password)
.IsRequired()
.HasColumnName("password")
.HasMaxLength(255);
entity.Property(e => e.Title)
.IsRequired()
.HasColumnName("title")
.HasMaxLength(10);
});
以及所有 'User' class:
public partial class User : DomainAppBase, IStorable
{
public User()
{
Leads = new HashSet<Lead>();
}
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Title { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public override void SetDefaultValues()
{
UserId = NullKey;
}
[NotMapped]
public ICollection<Lead> Leads { get; set; }
[NotMapped]
public new int Key => UserId;
}
任何帮助或指点将不胜感激。提前致谢。
请将模型构建器更新为
modelBuilder.Entity<User>(entity =>
{
entity.HasKey(o => o.UserId );
entity.Property(e => e.UserId).ValueGeneratedOnAdd().HasColumnName("user_id")
...................
ValueGeneratedOnAdd
允许在添加新实体时生成一个值。