简体   繁体   English

当已经实现了自动增量时,如果将identity_insert设置为OFF,则无法为标识列插入显式值

[英]Cannot insert explicit value for identity column when identity_insert is set to OFF when auto-increment is already implemented

I have already implemented the auto-increment for database, the identity_insert is set to OFF. 我已经实现了数据库的自动增量,identity_insert设置为OFF。

However, when I tried to insert data into the other columns, I get the "Cannot insert explicit value for identity column in table 'Bookmarks' when IDENTITY_INSERT is set to OFF." 但是,当我尝试将数据插入其他列时,出现“当IDENTITY_INSERT设置为OFF时,无法在表'书签'中为身份列插入显式值”。 error. 错误。 The code below is the table definition: 下面的代码是表定义:

CREATE TABLE [dbo].[Bookmarks] (
[Id]        INT           IDENTITY (1, 1) NOT NULL,
[carparkId] INT           NULL,
[date]      DATETIME      NULL,
[username]  NVARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC),
FOREIGN KEY ([carparkId]) REFERENCES [dbo].[Carparks] ([id])

); );

The code below shows the inserting of the data and the error occurs on db.SaveChanges(). 下面的代码显示了数据的插入,并且错误发生在db.SaveChanges()上。

    public void Insert(Bookmark obj)
    {
        data.Add(obj);
        db.SaveChanges();
    }

This is the data i am trying to pass in: 这是我要传递的数据:

 public ActionResult Create([Bind(Include = "carparkId,date,username")] Bookmark bookmark)
    {
        if (ModelState.IsValid)
        {

            bookmarkGateway.Insert(bookmark);
            return RedirectToAction("Index");

        }

Any idea how to solve this error without turning on the identity_insert ? 任何想法如何解决此错误而无需打开identity_insert吗? Your help is much appreciated thanks. 非常感谢您的帮助。

From the code you posted seems that error in EF mapping. 从您发布的代码看来,EF映射中的错误。

class BookmarkMap: EntityTypeConfiguration<Bookmark>
{
    public BookmarkMap()
    {
        Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
    }
}

暂无
暂无

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

相关问题 实体框架:当IDENTITY_INSERT设置为OFF时,无法为表&#39;[table]&#39;中的标识列插入显式值 - Entity Framework: Cannot insert explicit value for identity column in table '[table]' when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,无法为表&#39;ActivityInstances中的标识列插入显式值 - Cannot insert explicit value for identity column in table 'ActivityInstances' when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,无法为标识列插入显式值 - Cannot insert explicit value for identity column 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 当IDENTITY_INSERT设置为OFF时,无法在表&#39;MyTableName&#39;中为标识列插入显式值 - Cannot insert explicit value for identity column in table 'MyTableName' 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 当IDENTITY_INSERT设置为OFF时,无法在表&#39;candidatedetails&#39;中为identity列插入显式值 - Cannot insert explicit value for identity column in table 'candidatedetails' when IDENTITY_INSERT is set to OFF 当 IDENTITY_INSERT 设置为 OFF 时,无法为标识列插入显式值。 (实体框架核心) - Cannot insert explicit value for identity column when IDENTITY_INSERT is set to OFF. (Entity Framework Core) IDENTITY_INSERT设置为OFF时,EF无法为表“ PATIENT DONOR”中的标识列插入显式值 - EF Cannot insert explicit value for identity column in table 'PATIENT DONOR' when IDENTITY_INSERT is set to OFF IDENTITY_INSERT设置为OFF时,1:1映射并且无法为表&#39;DivisionParticipant&#39;中的Identity列插入显式值 - 1:1 mapping and Cannot insert explicit value for identity column in table 'DivisionParticipant' when IDENTITY_INSERT is set to OFF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM