Entity Framework Core: 外键可能会在迁移中导致循环错误

huangapple go评论93阅读模式
英文:

Entity Framework Core: FOREIGN KEY may cause cycles error in a migration

问题

我有"制造"和"照片"类。我想在"制造"类中存储"Logo"(照片)和"Cover"(照片)属性。但当我尝试添加迁移时,它给我以下错误:

引入FOREIGN KEY约束'FK_Manufactures_Photos_LogoId'到表'Manufactures'可能导致循环或多重级联路径。请指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

我该如何修复这个问题?

制造类:

  1. public class Manufacture : BaseEntity
  2. {
  3. public Manufacture()
  4. {
  5. Logo = new Photo();
  6. CoverPhoto = new Photo();
  7. SocialMediaAccounts = new List<SocialMediaAccount>();
  8. Group = new Group();
  9. }
  10. public Photo Logo { get; set; }
  11. public int LogoId { get; set; }
  12. public Photo CoverPhoto { get; set; }
  13. public string? Name { get; set; }
  14. public string? Description { get; set; }
  15. public int GroupId { get; set; }
  16. public Group Group { get; set; }
  17. public string? Website { get; set; }
  18. public List<SocialMediaAccount> SocialMediaAccounts { get; set; } = new List<SocialMediaAccount>();
  19. }

照片类:

  1. public class Photo : BaseEntity
  2. {
  3. public string? Url { get; set; }
  4. }

基础实体类用于任何额外信息:

  1. public abstract class BaseEntity
  2. {
  3. public int Id { get; set; }
  4. public DateTime ObjectCreatedDate { get; set; }
  5. public DateTime ObjecLastModifiedDate { get; set; }
  6. private ICollection<INotification> domainEvents;
  7. public ICollection<INotification> DomainEvents => domainEvents;
  8. public void AddDomainEvents(INotification notification)
  9. {
  10. domainEvents ??= new List<INotification>();
  11. domainEvents.Add(notification);
  12. }
  13. }
英文:

I have Manufacture and Photo classes. I want to store "Logo" (Photo) and "Cover" (Photo) properties in Manufacture class. But when I try to add-migration, it gives me the below error:

> Introducing FOREIGN KEY constraint 'FK_Manufactures_Photos_LogoId' on table 'Manufactures' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

How should I fix this?

Class Manufacture:

  1. public class Manufacture : BaseEntity
  2. {
  3. public Manufacture()
  4. {
  5. Logo = new Photo();
  6. CoverPhoto = new Photo();
  7. SocialMediaAccounts = new List&lt;SocialMediaAccount&gt;();
  8. Group = new Group();
  9. }
  10. public Photo Logo { get; set; }
  11. public int LogoId { get; set; }
  12. public Photo CoverPhoto { get; set; }
  13. public string? Name { get; set; }
  14. public string? Description { get; set; }
  15. public int GroupId { get; set; }
  16. public Group Group { get; set; }
  17. public string? Website { get; set; }
  18. public List&lt;SocialMediaAccount&gt; SocialMediaAccounts { get; set; } = new List&lt;SocialMediaAccount&gt;();
  19. }

class Photo:

  1. public class Photo:BaseEntity
  2. {
  3. public string? Url { get; set; }
  4. }

class BaseEntity for any extra info:

  1. public abstract class BaseEntity
  2. {
  3. public int Id { get; set; }
  4. public DateTime ObjectCreatedDate { get; set; }
  5. public DateTime ObjecLastModifiedDate { get; set; }
  6. private ICollection&lt;INotification&gt; domainEvents;
  7. public ICollection&lt;INotification&gt; DomainEvents =&gt; domainEvents;
  8. public void AddDomainEvents(INotification notification)
  9. {
  10. domainEvents ??= new List&lt;INotification&gt;();
  11. domainEvents.Add(notification);
  12. }
  13. }

I designed tables like this before, but I do not find what is the problem.

答案1

得分: 1

你在制造表格与照片表格之间有两个关联关系(logo、CoverPhoto),默认采用级联删除类型。你有两种解决方法:

  1. 更改级联删除类型中的一个类

在DbContext中:

  1. protected override void OnModelCreating(ModelBuilder modelBuilder)
  2. {
  3. modelBuilder.Entity<Manufacture>()
  4. .HasOne(u => u.Logo)
  5. .WithOne()
  6. .OnDelete(DeleteBehavior.Restrict);
  7. }

级联删除类型的选项:

  • Cascade - 依赖项将被删除
  • Restrict - 依赖项不受影响
  • SetNull - 依赖行中的外键值将更新为NULL

EF Core OnDelete

  1. 使其中一个类可以为可空类型。
英文:

You have two releation from the Manufacture table to the photo table(logo,CoverPhoto), which defaults to the Cascade delete type
You have two ways to solve this

1.change type Cascade delete one of classes

In DbContext

  1. protected override void OnModelCreating(ModelBuilder modelBuilder)
  2. {
  3. modelBuilder.Entity&lt;Manufacture&gt;()
  4. .HasOne(u =&gt; u.Logo)
  5. .WithOne()
  6. .OnDelete(DeleteBehavior.Restrict);

Cascade - dependents should be deleted
Restrict - dependents are unaffected
SetNull - the foreign key values in dependent rows should update to NULL

EF Core OnDelete

  1. one of the class that can be nullable

huangapple
  • 本文由 发表于 2023年6月19日 21:39:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定