EF Core-添加相关实体时出错 [英] EF Core - Error when adding a related entity
问题描述
当我尝试更新已经从数据库获取的实体的相关实体时,出现错误.出于说明目的,我有以下实体:
I get an error when I try to update a related entity of an entity that I already got from database. For illustration purposes I have these entites:
class Car
{
int Id ..;
string Name ..;
virtual ICollection<TireCar> tires ...;
}
class TireCar
{
int Id ..;
int TireId ..;
int CarId..;
int Size..;
virtual TireBrand tire;
virtual Car car;
}
class TireBrand
{
int Id;
string Name ..;
}
因此,我正在尝试制作一个Patch方法,该方法允许我更新Car
数据并添加,更新或删除tires
.当我得到Car
实体,然后添加Tire
时,就会发生问题.像这样的东西:
So, I'm trying to make a Patch method that allows me to update the Car
data and also adds, updates or deletes the tires
. The problem happens when I get the Car
entity and after that I add a Tire
. Something like that:
void UpdateCar()
{
var car = carService.Get(...);
...
carService.AddTire(new TireCar{ CarId = car.Id, TireId = 1 });
...
}
我正在使用带有DI的Repository模式,因此上下文是相同的.抛出的错误是:
I'm using the Repository pattern with DI, so the context is the same. The error that is throwing is:
System.InvalidOperationException::实体类型"Car"和"TireCar"之间的关联已被切断,但是该关系的外键不能设置为null.如果应删除从属实体,则将关系设置为使用级联删除.'
System.InvalidOperationException: The association between entity types 'Car' and 'TireCar' has been severed but the foreign key for this relationship cannot be set to null. If the dependent entity should be deleted, then setup the relationship to use cascade deletes.'
我尝试了两种可行的方法,但我认为这不是解决方案:
I tried two things that worked but I think is not the solution:
- 添加轮胎后上车
- 当我没有追踪到汽车时
如果我要更新另一个表,为什么会发生这种情况?我该怎么办才能解决这个问题?
Why that happend if I'm updating another tables? What Can I do to solve this?
推荐答案
您的问题实际上是配置问题,而不是EF Core的问题.仔细阅读错误内容:
Your issue is actually a configuration issue, not an issue with EF Core. Read carefully what the error says:
实体类型"Car"和"TireCar"之间的关联已被切断,但该关系的外键不能设置为空.如果应删除从属实体,则将关系设置为使用级联删除.
The association between entity types 'Car' and 'TireCar' has been severed but the foreign key for this relationship cannot be set to null. If the dependent entity should be deleted, then setup the relationship to use cascade deletes.
实体框架核心默认情况下(.NET Core 2.0转发)具有 SET NULL 策略.如果我们仔细查看您的TireCar
模型,您没有将CarId
属性设置为可为空,则该列不能设置为空.
Entity Framework Core has by default (.NET Core 2.0 forward) a SET NULL policy when a dependent entity becomes orphan. If we look carefully at your TireCar
model, you are not setting the CarId
property as nullable, so the column cannot be set to null.
您有两种不同的解决方案来解决此问题.如果要在从Car
实体中删除TireCar
实体时将其删除,则可以为此关系设置级联删除(您也可以更改EF Core默认策略).可以通过FluentApi
在DbContext
中设置.
You have two different solutions to fix this. If you want the TireCar
entity to get deleted when you remove it from your Car
entity, then setup cascade deletes for this relationship (You can also change the EF Core default policy). This can be setup in your DbContext
via FluentApi
.
class MyContext : DbContext
{
// Your DbSets
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<TireCar>()
.HasOne(tc => tc.car)
.WithMany(car => car.tires)
.OnDelete(DeleteBehavior.Cascade);
}
}
另一种解决方案是让TireCar
实体在CarId
列中具有null
值,只需像这样更改您的实体模型.
Another solution is to let the TireCar
entity have null
values in the CarId
column, simply changing your entity model like this.
class TireCar
{
public int Id { get; set; }
public int TireId { get; set; }
public int? CarId { get; set; }
public int Size { get; set; }
public virtual TireBrand tire { get; set; }
public virtual Car car { get; set; }
}
这篇关于EF Core-添加相关实体时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!