简体   繁体   English

ASP.NET MVC 错误 SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表“用户”中插入标识列的显式值

[英]ASP.NET MVC error SqlException: Cannot insert explicit value for identity column in table 'Users' when IDENTITY_INSERT is set to OFF

I'm trying to make a login AUTH with C# .NET but I'm getting this error and can't solve it.我正在尝试使用 C# .NET 进行登录身份验证,但出现此错误并且无法解决。

Here is my code.这是我的代码。

User class User

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }

RegisterDTO注册DTO

    public string Name { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
 

IUserRepository IUserRepository

public class UserRepository : IUserRepository
{
    private readonly ApiDbContext _context;

    public UserRepository(ApiDbContext context) 
    {
        _context = context;
    }

    public User Create(User user)
    {
        _context.Users.Add(user);
        user.UserId =_context.SaveChanges();
        return user;
    }
}

And the error I'm getting is when I start Swagger and trying to post username email and password:我得到的错误是当我启动 Swagger 并尝试发布用户名电子邮件和密码时:

SqlException: Cannot insert explicit value for identity column in table 'Users' when IDENTITY_INSERT is set to OFF. SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表“用户”中插入标识列的显式值。

In your database, your Users table is defined as在您的数据库中,您的用户表定义为

CREATE TABLE Users (
...
UserId int identity(1,1)
...
)

This means that your database wants to generate the UserId value for you, unless Identity_Insert=off is specified when a record in inserted.这意味着您的数据库想要为您生成 UserId 值,除非在插入记录时指定 Identity_Insert=off。 However, when you are calling Create(), the query that is being executed includes the UserId column.但是,当您调用 Create() 时,正在执行的查询包括 UserId 列。

You don't state in your question (or your tags) that your are using EntityFramework, but from your syntax, I am making that assumption.您没有在您的问题(或您的标签)中说明您正在使用 EntityFramework,但从您的语法来看,我正在做出这个假设。 As such, you should specify this in your OnModelCreating method.因此,您应该在 OnModelCreating 方法中指定它。

protected override void OnModelCreating(ModelBuilder modelBuilder) 
{
...
modelBuilder.Entity<Users>(Entity =>
  {
...
    Entity.Property(e=>e.UserId).ValueGeneratedOnAdd();
...
  }
...
}

You most likely generated the model before adding the [DatabaseGenerated(DatabaseGeneratedOption.Identity)] attribute.您很可能在添加 [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 属性之前生成了模型。 And its being cached in your *.edmx file.它被缓存在您的 *.edmx 文件中。

I had such experience, and I was able to resolve by clearing out EF cache and *.edmx files, hence forcing the regeneration of my models.我有这样的经验,我能够通过清除 EF 缓存和 *.edmx 文件来解决,从而迫使我的模型重新生成。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.net MVC 错误:当 IDENTITY_INSERT 设置为 OFF 时,无法在表“公司”中插入标识列的显式值 - ASP.net MVC Error: Cannot insert explicit value for identity column in table 'Company' when IDENTITY_INSERT is set to OFF SqlException:当 IDENTITY_INSERT 设置为 OFF ASP.NET Core 2.1 时,无法在表“类别”中插入标识列的显式值 - SqlException: Cannot insert explicit value for identity column in table 'Categories' when IDENTITY_INSERT is set to OFF ASP.NET Core 2.1 SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法为表 [表名] 中的标识列插入显式值 - SqlException: Cannot insert explicit value for identity column in table [Table name] when IDENTITY_INSERT is set to OFF SqlException:当IDENTITY_INSERT设置为OFF时,无法为表&#39;Recipe&#39;中的Identity列插入显式值 - SqlException: Cannot insert explicit value for identity column in table 'Recipe' when IDENTITY_INSERT is set to OFF SqlException:当IDENTITY_INSERT设置为OFF时,无法为表&#39;AspNetUsers&#39;中的Identity列插入显式值 - SqlException: Cannot insert explicit value for identity column in table 'AspNetUsers' when IDENTITY_INSERT is set to OFF SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表 'Ogretmenler' 中为标识列插入显式值 - SqlException: Cannot insert explicit value for identity column in table 'Ogretmenler' when IDENTITY_INSERT is set to OFF SqlException:当IDENTITY_INSERT设置为OFF时,无法为表&#39;&#39;中的标识列插入显式值 - SqlException: Cannot insert explicit value for identity column in table '' when IDENTITY_INSERT is set to OFF SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表“任务”中插入标识列的显式值 - SqlException: Cannot insert explicit value for identity column in table 'Tasks' when IDENTITY_INSERT is set to OFF 出现错误:当IDENTITY_INSERT设置为OFF时,无法在表&#39;Table&#39;中为标识列插入显式值 - Getting Error: Cannot insert explicit value for identity column in table 'Table' when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,无法在TABLE中为Identity列插入显式值; .NET Core 2.1 - Cannot insert explicit value for identity column in TABLE when IDENTITY_INSERT is set to OFF; .NET Core 2.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM