“如何修复无法跟踪‘实体类型的实例’,因为已经跟踪了具有键值‘{TypeId: 1}’的另一个实例。
“How to fix ‘The instance of entity type ' cannot be tracked because another instance with the key value '{TypeId: 1}' is already being tracked.
无法跟踪实体类型 'WalletType' 的实例,因为已在跟踪具有键值“{TypeId: 1}”的另一个实例。附加现有实体时,确保只附加一个具有给定键值的实体实例。
//WalletType.cs
public class 钱包类型
{
public WalletType()
{
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int TypeId { get; set; }
[MaxLength(150)]
public string TypeTitle { get; set; }
public virtual ICollection<Wallet> Wallets { get; set; }
}
//////////////////////////////
//SeedData.cs
public class 种子数据
{
public static void Initialize(IServiceProvider serviceProvider)
{
使用 (var context = new ApplicationDbContext(
serviceProvider.GetRequiredService>()))
{
// 寻找任何电影。
如果 (context.WalletTypes.Any())
{
return; // 数据库已经播种
}
context.WalletTypes.AddRange(
new WalletType
{
TypeId = 1,
TypeTitle = "function1"
},
new WalletType
{
TypeId = 1,
TypeTitle = "function2"
}
);
context.SaveChanges();
}
}
}
////////////////////////////////////
//Program.cs
public class 计划
{
public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args).Build();
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.
GetRequiredService<ApplicationDbContext>();
context.Database.Migrate();
SeedData.Initialize(services);
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred seeding the DB.");
}
}
host.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
那是因为你添加到 WalletType 时使用了 TypeId。
您可以将标识设置为自动或手动提供唯一值。
context.WalletTypes.AddRange(
new WalletType
{
TypeId = 1,
TypeTitle = "function1"
},
new WalletType
{
TypeId = 2,
TypeTitle = "function2"
}
);
无法跟踪实体类型 'WalletType' 的实例,因为已在跟踪具有键值“{TypeId: 1}”的另一个实例。附加现有实体时,确保只附加一个具有给定键值的实体实例。
//WalletType.cs public class 钱包类型
{
public WalletType()
{
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int TypeId { get; set; }
[MaxLength(150)]
public string TypeTitle { get; set; }
public virtual ICollection<Wallet> Wallets { get; set; }
}
////////////////////////////// //SeedData.cs public class 种子数据 { public static void Initialize(IServiceProvider serviceProvider) { 使用 (var context = new ApplicationDbContext( serviceProvider.GetRequiredService>())) { // 寻找任何电影。 如果 (context.WalletTypes.Any()) { return; // 数据库已经播种 }
context.WalletTypes.AddRange(
new WalletType
{
TypeId = 1,
TypeTitle = "function1"
},
new WalletType
{
TypeId = 1,
TypeTitle = "function2"
}
);
context.SaveChanges();
}
}
}
//////////////////////////////////// //Program.cs public class 计划 {
public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args).Build();
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.
GetRequiredService<ApplicationDbContext>();
context.Database.Migrate();
SeedData.Initialize(services);
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred seeding the DB.");
}
}
host.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
那是因为你添加到 WalletType 时使用了 TypeId。 您可以将标识设置为自动或手动提供唯一值。
context.WalletTypes.AddRange(
new WalletType
{
TypeId = 1,
TypeTitle = "function1"
},
new WalletType
{
TypeId = 2,
TypeTitle = "function2"
}
);