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

时间:2018-01-20 11:27:42

标签: c# entity-framework .net-core

使用下面的代码,我得到的错误表明我需要将[身份证明] [游戏]和[游戏] [游戏]中的身份插入设置为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();
        }

如果我添加了同时设置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();
        }

错误,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'.'

我不知道该怎么做,因为我甚至没有更改锦标赛和比赛桌,只有项目。知道如何解决这个问题吗?

更新:

我通过删除所有迁移,禁用初始化方法,然后重做迁移来解决身份插入问题。

我正面临另一个问题:

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();
        }

最后一行抛出SqlException: Invalid object name 'Game'.

模特:

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; }
}

我的背景

 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);
    }

}

游戏不为空,并且匹配正确。

1 个答案:

答案 0 :(得分:0)

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