gpt4 book ai didi

entity-framework - 无法确定关系的主体端 - 多个添加的实体可能具有相同的主键

转载 作者:行者123 更新时间:2023-12-04 07:24:02 26 4
gpt4 key购买 nike

我正在使用 Entity Framework 5 Code First,并且我有以下适用于汽车制造商、汽车和卡车的模型:

public class Manufacturer
{
public int Id { get; set; }
public string Name { get; set; }

[ForeignKey("ManufacturerId")]
public virtual List<Car> Cars { get; set; }
[ForeignKey("ManufacturerId")]
public virtual List<Truck> Trucks { get; set; }
}

public class Vehicle
{
public int Id { get; set; }
public string Colour { get; set; }
public int ManufacturerId { get; set; }
public virtual Manufacturer Manufacturer { get; set; }
}

public class Car : Vehicle
{ }

public class Truck : Vehicle
{ }

public class Context : DbContext
{
public DbSet<Manufacturer> Manufacturers { get; set; }
public DbSet<Vehicle> Vehicles { get; set; }
}

对于开发和测试,我将数据播种如下:

public class DbInitialiser : DropCreateDatabaseAlways<Context>
{
protected override void Seed(Context context)
{
var manufacturers = new List<Manufacturer>
{
new Manufacturer
{
Name = "Test Manufacturer",
Cars = new List<Car>
{
new Car { Colour = "Blue" },
new Car { Colour = "Green" }
},
Trucks = new List<Truck>
{
new Truck { Colour = "Red" }
}
},
new Manufacturer
{
Name = "Another Manufacturer",
Cars = new List<Car>
{
new Car { Colour = "Pink" }
}
}
};

manufacturers.ForEach(x => context.Manufacturers.Add(x));
}
}

但是,当我使用上下文时,出现以下异常:
无法确定“EF_Associations.Vehicle_Manufacturer”关系的主体端。多个添加的实体可能具有相同的主键。

堆栈跟踪:
System.Data.DataException was unhandled
HResult=-2146233087
Message=An exception occurred while initializing the database. See the InnerException for details.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c)
at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
at EF_Associations.Program.Main(String[] args) in c:\temp\EF-Associations\Program.cs:line 19
...
InnerException: System.Data.Entity.Infrastructure.DbUpdateException
HResult=-2146233087
Message=Unable to determine the principal end of the 'EF_Associations.Vehicle_Manufacturer' relationship. Multiple added entities may have the same primary key.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at System.Data.Entity.DropCreateDatabaseAlways`1.InitializeDatabase(TContext context)
at System.Data.Entity.Database.<>c__DisplayClass2`1.<SetInitializerInternal>b__0(DbContext c)
at System.Data.Entity.Internal.InternalContext.<>c__DisplayClass8.<PerformDatabaseInitialization>b__6()
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
InnerException: System.Data.UpdateException
HResult=-2146233087
Message=Unable to determine the principal end of the 'EF_Associations.Vehicle_Manufacturer' relationship. Multiple added entities may have the same primary key.
Source=System.Data.Entity
StackTrace:
at System.Data.Mapping.Update.Internal.UpdateTranslator.RegisterEntityReferentialConstraints(IEntityStateEntry stateEntry, Boolean currentValues)
at System.Data.Mapping.Update.Internal.UpdateTranslator.RegisterReferentialConstraints(IEntityStateEntry stateEntry)
at System.Data.Mapping.Update.Internal.UpdateTranslator.PullModifiedEntriesFromStateManager()
at System.Data.Mapping.Update.Internal.UpdateTranslator.ProduceCommands()
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
InnerException:

我将如何指定这种关系的主要结束点?

我尝试通过模型构建器 fluent API 添加以下配置,但没有成功:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Vehicle>()
.HasRequired(v => v.Manufacturer)
.WithRequiredDependent();
}

最佳答案

您的制造商类中必须只有一个外键属性:Vehicles。

[ForeignKey("ManufacturerId")]
public virtual List<Vehicle> Vehicles { get; set; }

您必须修改您的种子方法以使用这个单一的 FK 属性。 EF 将创建一个从实体类的继承链推断出的每个层次结构的表模型:
protected override void Seed(Context context)
{
var manufacturers = new List<Manufacturer>
{
new Manufacturer
{
Name = "Test Manufacturer",
Vehicles = new List<Vehicle>
{
new Car {Colour = "Blue" },
new Car {Colour = "Green" },
new Truck {Colour = "Red" }
}
},
new Manufacturer
{
Name = "Another Manufacturer",
Vehicles = new List<Vehicle>
{
new Car {Colour = "Pink" }
}
}
};

manufacturers.ForEach(x => context.Manufacturers.Add(x));
context.SaveChanges();
}

按照惯例,EF 将生成一个额外的 鉴别器 列的值取自您的 中的子类名称( Car , Truck )车辆 table 。

为方便起见,您可以在制造商上创建两个额外的(非 FK)属性,每个属性都包含一个特定于继承的 OfType询问:
public IEnumerable<Car> Cars
{
get
{
return this.Vehicles.OfType<Car>();
}
}

public IEnumerable<Truck> Trucks
{
get
{
return this.Vehicles.OfType<Truck>();
}
}

并使用它们来查询:
using (var db = new Context())
{
foreach (var manufacturer in db.Manufacturers)
{
var cars = manufacturer.Cars.ToList();
var trucks = manufacturer.Trucks.ToList();
}
}

关于entity-framework - 无法确定关系的主体端 - 多个添加的实体可能具有相同的主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18743806/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com