简体   繁体   English

SqlException:当IDENTITY_INSERT设置为OFF时,无法为表''中的标识列插入显式值

[英]SqlException: Cannot insert explicit value for identity column in table '' when IDENTITY_INSERT is set to OFF

Using the following code, I get the error that states I need to put identity insert to ON for [Tournaments].[Tournaments] and [Games].[Game] 使用以下代码,我得到一个错误,指出我需要将[Tournaments]。[Tournaments]和[Games]。[Game]的身份插入置于ON。

 if (!context.Items.Any())
        {
            context.Database.OpenConnection();
            context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [Items].[Items] ON;");
            Tournament Tournament = _tournamentDbContext.Tournaments.Where(x => x.Id == 1).First();
            Game _Game = _gameDbContext.Games.Where(x => x.Id == 1).First();
            ItemType ItemType = context.ItemTypes.Where(x => x.Id == 1).First();
            Item item = new Models.Items.Item
            {
                Id = 1,
                Name = "Test",
                ItemType = ItemType,
                Price = 100,
                Short_Description = "TestShort",
                Tournament = Tournament,
                Game = _Game,
                DateCreated = DateTime.Now
            };
            context.Items.Add(item);
            context.SaveChanges();
            context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [Items].[Items] OFF;");
            context.Database.CloseConnection();
        }

If I add the code which sets both identity_inserts on: 如果我添加将两个identity_inserts设置为以下代码:

  if (!context.Items.Any())
        {
            context.Database.OpenConnection();
            context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [Items].[Items] ON;");
            context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [Tournaments].[Tournaments] ON;");
            context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [Game].[Games] ON;");
            Tournament Tournament = _tournamentDbContext.Tournaments.Where(x => x.Id == 1).First();
            Game _Game = _gameDbContext.Games.Where(x => x.Id == 1).First();
            ItemType ItemType = context.ItemTypes.Where(x => x.Id == 1).First();
            Item item = new Models.Items.Item
            {
                Id = 1,
                Name = "Test",
                ItemType = ItemType,
                Price = 100,
                Short_Description = "TestShort",
                Tournament = Tournament,
                Game = _Game,
                DateCreated = DateTime.Now
            };
            context.Items.Add(item);
            context.SaveChanges();
            context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [Items].[Items] OFF;");
            context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [Tournaments].[Tournaments] OFF;");
            context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [Game].[Games] OFF;");
            context.Database.CloseConnection();
        }

It errors, System.Data.SqlClient.SqlException: 'IDENTITY_INSERT is already ON for table 'aspnet-SpidShop-FA900737-72CD-4ABA-BDEF-598D51CB5A43.Items.Items'. Cannot perform SET operation for table 'Tournaments.Tournaments'.' 它发生错误, System.Data.SqlClient.SqlException: 'IDENTITY_INSERT is already ON for table 'aspnet-SpidShop-FA900737-72CD-4ABA-BDEF-598D51CB5A43.Items.Items'. Cannot perform SET operation for table 'Tournaments.Tournaments'.' System.Data.SqlClient.SqlException: 'IDENTITY_INSERT is already ON for table 'aspnet-SpidShop-FA900737-72CD-4ABA-BDEF-598D51CB5A43.Items.Items'. Cannot perform SET operation for table 'Tournaments.Tournaments'.'

I have no idea what to do, as I'm not even changing Tournaments and Games tables, only Items. 我不知道该怎么办,因为我什至不更改锦标赛和游戏表,仅更改项目。 Any idea how to fix this issue? 任何想法如何解决此问题?

UPDATE: 更新:

I got through the identity insert problems by deleting all migrations, disabling initialize method, then redoing the migrations. 我通过删除所有迁移,禁用初始化方法,然后重做迁移来解决身份插入问题。

I'm facing another problem: 我面临另一个问题:

if (!context.Items.Any())
        {
            Tournament Tournament = _tournamentDbContext.Tournaments.Where(x => x.Id == 1).First();
            Game Game = _gameDbContext.Games.Where(x => x.Id == 1).First();
            ItemType ItemType = context.ItemTypes.Where(x => x.Id == 1).First();
            Item item = new Models.Items.Item
            {
                Name = "Test",
                ItemType = ItemType,
                Price = 100,
                Short_Description = "TestShort",
                Tournament = Tournament,
                Game = Game,
                DateCreated = DateTime.Now
            };
            context.Items.Add(item);
            context.SaveChanges();
        }

The last line throws SqlException: Invalid object name 'Game'. 最后一行抛出SqlException: Invalid object name 'Game'.

The model: 该模型:

public class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Short_Description { get; set; }
    public decimal Price { get; set; }
    public ItemType ItemType { get; set; }
    public Tournament Tournament { get; set; }
    public Game Game { get; set; }
    [DataType(DataType.Date)]
    public DateTime DateCreated { get; set; }
}

And my context 而我的背景

 public class ItemDbContext : DbContext
{
    private string Schema = "Items";
    public ItemDbContext(DbContextOptions<ItemDbContext> options) : base(options)
    {
    }

    public DbSet<Item> Items { get; set; }
    public DbSet<ItemType> ItemTypes { get; set; }
    public DbSet<ItemTag> ItemTags { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Item>().ToTable("Item", Schema);
        modelBuilder.Entity<ItemType>().ToTable("ItemType", Schema);
        modelBuilder.Entity<ItemTag>().ToTable("ItemTag", Schema);
    }

}

Game is not null, and it gets matched properly. 游戏不为空,并且可以正确匹配。

我有多个上下文,将所有上下文放在一起,并删除了ID,因为它们应该自动生成。

暂无
暂无

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

相关问题 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 时,无法在表 'Ogretmenler' 中为标识列插入显式值 - SqlException: Cannot insert explicit value for identity column in table 'Ogretmenler' 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 时,无法在表“任务”中插入标识列的显式值 - SqlException: Cannot insert explicit value for identity column in table 'Tasks' when IDENTITY_INSERT is set to OFF 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 EF Core SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表“MenuCategories”中插入标识列的显式值 - EF Core SqlException: Cannot insert explicit value for identity column in table 'MenuCategories' 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 实体框架:当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;Table&#39;中为标识列插入显式值 - Getting Error: Cannot insert explicit value for identity column in table 'Table' when IDENTITY_INSERT is set to OFF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM