万一有人还在迷茫。. .
有关使 IDENTITY_INSERT 与 Code-First Migration Seed() 方法一起使用所需的其他信息,请参见下文
我确实使用 Aron 的System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated
属性实现将模型 ID 的 DB 生成属性设置为“无”,但我仍然无法通过身份插入错误。我想我会在这里发布我的发现,以防其他人仍然遇到问题。
为了让它工作,我将种子方法的逻辑包装在一个 SQL 事务中,并用于在运行该方法context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT myTable ON")
之前允许插入。.AddOrUpdate()
这是我的 Configuration.vb 文件(使用 Google API 类型的表作为我们的示例数据):
Imports System
Imports System.Data.Entity
Imports System.Data.Entity.Migrations
Imports System.Linq
Namespace Migrations
Friend NotInheritable Class Configuration
Inherits DbMigrationsConfiguration(Of DAL.MyDbContext)
Public Sub New()
AutomaticMigrationsEnabled = False
AutomaticMigrationDataLossAllowed = False
End Sub
Protected Overrides Sub Seed(context As DAL.MyDbContext)
' This method will be called after migrating to the latest version.
Dim newContext As New MyDbContext(context.Database.Connection.ConnectionString)
Using ts = newContext.Database.BeginTransaction()
Try
' Turn on identity insert before updating
newContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT GoogleApiTypeGroups ON")
' Make sure the expected GoogleApiTypeGroups exist with the correct names and IDs.
newContext.GoogleApiTypeGroups.AddOrUpdate(
Function(x) x.Id,
New GoogleApiTypeGroup() With {.Id = 1, .name = "Google Cloud APIs"},
New GoogleApiTypeGroup() With {.Id = 2, .name = "YouTube APIs"},
New GoogleApiTypeGroup() With {.Id = 3, .name = "Google Maps APIs"},
New GoogleApiTypeGroup() With {.Id = 4, .name = "Advertising APIs"},
New GoogleApiTypeGroup() With {.Id = 5, .name = "Google Apps APIs"},
New GoogleApiTypeGroup() With {.Id = 6, .name = "Other popular APIs"},
New GoogleApiTypeGroup() With {.Id = 7, .name = "Mobile APIs"},
New GoogleApiTypeGroup() With {.Id = 8, .name = "Social APIs"})
' Attempt to save the changes.
newContext.SaveChanges()
' Turn off the identity insert setting when done.
newContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT GoogleApiTypeGroups OFF")
' Turn on identity insert before updating
newContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT GoogleApiTypes ON")
' Make sure the expected GoogleApiTypes exist with the correct names, IDs, and references to their corresponding GoogleApiTypeGroup.
newContext.GoogleApiTypes.AddOrUpdate(
Function(x) x.Id,
New GoogleApiType() With {.Id = 1, .name = "Google Maps JavaScript API", .GoogleApiTypeGroupId = 3})
' Save the changes
newContext.SaveChanges()
' Turn off the identity insert setting when done.
newContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT GoogleApiTypes ON")
ts.Commit()
Catch ex As Exception
ts.Rollback()
Throw
End Try
End Using
End Sub
End Class
End Namespace