[英]Entity Framework Core code-first foreign key with optional linked entity
我有一个文件表,我想在其他 2 个表中引用它。 每个文件都是其中一个表独有的,这意味着我的外键在至少一个链接表中没有实体,某些文件可能有一个填充键,它根本没有链接到任何实体(因为它将是稍后装箱)。
关系:(1 -> n)
Item <- File -> Image
这会导致 insert/ SaveChanges
上的外键异常,因为数据库无法找到链接的实体。
我搜索了一个解决方案,但找不到任何解决此问题的文章,而且我提出的解决方案至少有一个代码异味。
问题:如何链接这 3 个表而不会出现数据库异常并产生代码/数据库气味?
还是整个数据架构有问题,我应该尝试一些不同的东西(如果有的话)?
我想出但不想使用的解决方案:
Files
表拆分为ItemFiles
和ImageFiles
(我听说这是 DB 的味道)其他信息:
缩短型号:
public class FileData
{
public Item Item { get; set; }
public ImageData Image { get; set; }
public Guid Id { get; set; }
public string HashKey { get; set; }
// ...
}
public class Item
{
public FileData[] Files { get; set; }
public Guid Id { get; set; }
public string HashKey { get; set; }
// ...
}
public class ImageData
{
public FileData[] Files { get; set; }
public Guid Id { get; set; }
public string HashKey { get; set; }
// ...
}
数据库配置:
public class FileDataConfiguration : IEntityTypeConfiguration<FileData>
{
public void Configure(EntityTypeBuilder<FileData> builder)
{
builder.HasKey(file => file.Id);
builder.HasIndex(file => file.HashKey);
// ...
}
}
public class ItemConfiguration : IEntityTypeConfiguration<Item>
{
public void Configure(EntityTypeBuilder<Item> builder)
{
builder.HasKey(item => item.Id);
builder.HasMany(item => item.Files)
.WithOne(file => file.Item)
.IsRequired(false)
.HasForeignKey(file => file.HashKey)
.IsRequired(false)
.HasPrincipalKey(item => item.HashKey);
builder.HasIndex(file => file.HashKey);
// ...
}
}
public class ImageDataConfiguration : IEntityTypeConfiguration<ImageData>
{
public void Configure(EntityTypeBuilder<ImageData> builder)
{
builder.HasKey(image => image.Id);
builder.HasMany(image => image.Files)
.WithOne(file => file.Image)
.IsRequired(false)
.HasForeignKey(file => file.HashKey)
.IsRequired(false)
.HasPrincipalKey(image => image.HashKey);
builder.HasIndex(image => image.HashKey);
// ...
}
}
此代码引发异常
// both examples throw an exception, independent of each other
//example 1:
dbContext.Files.Add(
new File(){
HashKey="1"
}
);
dbContext.SaveChanges();
//example 2:
dbContext.Files.Add(
new File(){
HashKey="2"
}
);
dbContext.Items.Add(
new Item(){
HashKey="2"
}
);
dbContext.SaveChanges();
通常,您的FileData
实体应包含Item
和ImageData
实体的Guid
外键,而不仅仅是导航属性,即Guid ItemId
和Guid ImageId
。 例如:
public class FileData
{
public Guid ItemId { get; set;}
public Item Item { get; set; }
public Guid ImageId {get; set; }
public ImageData Image { get; set; }
public Guid Id { get; set; }
public string HashKey { get; set; }
// ...
}
同样在 Fluent Api 中同时配置Item
和ImageData
时,此配置应该足够了
builder.HasMany(item => item.Files)
.WithOne(file => file.Item)
.OnDelete(DeleteBehavior.NoAction);
最后我决定放弃外键,因为我不需要更新其他实体并手动启动文件查询。 似乎不可能将文件添加为属性,因为错误一直被抛出(我尝试了这个解决方案) 。
使用的约束列表:Item.Id => Key Item.Hash => Indexed, Unique
Image.Id => 关键 Image.Hash => 索引,唯一
File.Id => 密钥 File.Hash => 索引,不唯一
我决定保留 Item 和 Image 的 ID 的原因之一是其他实体主要通过 ID 引用这些,而不是 hash 和 hash 可能会更改,需要大量更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.