efcore 实体配置_EF Core-添加相关实体时出错

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 tires ...;

}

class TireCar

{

int Id ..;

int TireId ..;

int CarId..;

int Size..;

virtual TireBrand tire;

virtual Car car;

}

class TireBrand

{

int Id;

string Name ..;

}

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

...

}

I'm using the Repository pattern with DI, so the context is the same. The error that is throwing is:

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:

When I get the Car after adding the Tires

When I get the Car as no tracking

Why that happend if I'm updating another tables? What Can I do to solve this?

解决方案

Your issue is actually a configuration issue, not an issue with EF Core. Read carefully what the error says:

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.

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.

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

.HasOne(tc => tc.car)

.WithMany(car => car.tires)

.OnDelete(DeleteBehavior.Cascade);

}

}

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值