英文:
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约束。
我该如何修复这个问题?
制造类:
public class Manufacture : BaseEntity
{
public Manufacture()
{
Logo = new Photo();
CoverPhoto = new Photo();
SocialMediaAccounts = new List<SocialMediaAccount>();
Group = new Group();
}
public Photo Logo { get; set; }
public int LogoId { get; set; }
public Photo CoverPhoto { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
public int GroupId { get; set; }
public Group Group { get; set; }
public string? Website { get; set; }
public List<SocialMediaAccount> SocialMediaAccounts { get; set; } = new List<SocialMediaAccount>();
}
照片类:
public class Photo : BaseEntity
{
public string? Url { get; set; }
}
基础实体类用于任何额外信息:
public abstract class BaseEntity
{
public int Id { get; set; }
public DateTime ObjectCreatedDate { get; set; }
public DateTime ObjecLastModifiedDate { get; set; }
private ICollection<INotification> domainEvents;
public ICollection<INotification> DomainEvents => domainEvents;
public void AddDomainEvents(INotification notification)
{
domainEvents ??= new List<INotification>();
domainEvents.Add(notification);
}
}
英文:
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:
public class Manufacture : BaseEntity
{
public Manufacture()
{
Logo = new Photo();
CoverPhoto = new Photo();
SocialMediaAccounts = new List<SocialMediaAccount>();
Group = new Group();
}
public Photo Logo { get; set; }
public int LogoId { get; set; }
public Photo CoverPhoto { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
public int GroupId { get; set; }
public Group Group { get; set; }
public string? Website { get; set; }
public List<SocialMediaAccount> SocialMediaAccounts { get; set; } = new List<SocialMediaAccount>();
}
class Photo:
public class Photo:BaseEntity
{
public string? Url { get; set; }
}
class BaseEntity for any extra info:
public abstract class BaseEntity
{
public int Id { get; set; }
public DateTime ObjectCreatedDate { get; set; }
public DateTime ObjecLastModifiedDate { get; set; }
private ICollection<INotification> domainEvents;
public ICollection<INotification> DomainEvents => domainEvents;
public void AddDomainEvents(INotification notification)
{
domainEvents ??= new List<INotification>();
domainEvents.Add(notification);
}
}
I designed tables like this before, but I do not find what is the problem.
答案1
得分: 1
你在制造表格与照片表格之间有两个关联关系(logo、CoverPhoto),默认采用级联删除类型。你有两种解决方法:
- 更改级联删除类型中的一个类
在DbContext中:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Manufacture>()
.HasOne(u => u.Logo)
.WithOne()
.OnDelete(DeleteBehavior.Restrict);
}
级联删除类型的选项:
- Cascade - 依赖项将被删除
- Restrict - 依赖项不受影响
- SetNull - 依赖行中的外键值将更新为NULL
- 使其中一个类可以为可空类型。
英文:
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
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Manufacture>()
.HasOne(u => u.Logo)
.WithOne()
.OnDelete(DeleteBehavior.Restrict);
Cascade - dependents should be deleted
Restrict - dependents are unaffected
SetNull - the foreign key values in dependent rows should update to NULL
- one of the class that can be nullable
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论