我想创建具有两个属性的表,这两个属性与同一个表的关系是一对一的。我的模特:
public class ImageFile : BaseEntity
{
[Key]
Guid Guid { get; set; }
public string Name { get; set; }
public string Extension { get; set; }
public long Size { get; set; }
public byte[] FileContent { get; set; }
public ProjectImage ProjectImage { get; set; }
}
public class ProjectImage : BaseEntity
{
public string Description { get; set }
public ImageFile Thumbnail { get; set; }
public ImageFile FullSizeImage { get; set; }
}
以下是我的背景信息:
modelBuilder.Entity<ProjectImage>()
.HasRequired(f => f.FullSizeImage)
.WithOptional(i => i.ProjectImage);
modelBuilder.Entity<ProjectImage>()
.HasRequired(f => f.Thumbnail)
.WithOptional(i => i.ProjectImage);
它创建一对多的数据库。为什么呢?
编辑V2:
public class ImageFile : BaseEntity
{
[Key]
Guid Guid { get; set; }
public string Name { get; set; }
public string Extension { get; set; }
public long Size { get; set; }
public byte[] FileContent { get; set; }
public Guid? ProjectImageGuid { get; set; }
[ForeignKey("ProjectImageGuid")]
public ProjectImage ProjectImage { get; set; }
}
public class ProjectImage : BaseEntity
{
public string Description { get; set }
public Guid? ThumbnailGuid { get; set; }
public Guid? FullSizeImageGuid { get; set; }
[ForeignKey("ThumbnailGuid")]
public ImageFile Thumbnail { get; set; }
[ForeignKey("FullSizeImageGuid")]
public ImageFile FullSizeImage { get; set; }
}
以下是我的背景信息:
modelBuilder.Entity<ProjectImage>()
.HasRequired(f => f.FullSizeImage)
.WithOptional(i => i.ProjectImage);
modelBuilder.Entity<ProjectImage>()
.HasRequired(f => f.Thumbnail)
.WithOptional(i => i.ProjectImage);
我有错误:
ProjectImage_Thumbnail_Source :: Multiplicity在Role中无效 关系中的'ProjectImage_Thumbnail_Source' 'ProjectImage_Thumbnail'。因为依赖角色属性是 不是关键属性,多重性的上限 依赖角色必须是'*'。
答案 0 :(得分:0)
试试这个:
public class ImageFile : BaseEntity
{
[Key]
Guid Guid { get; set; }
public string Name { get; set; }
public string Extension { get; set; }
public long Size { get; set; }
public byte[] FileContent { get; set; }
public Guid ProjectImageGuid { get; set; }
[ForeignKey("ProjectImageGuid")]
public ProjectImage ProjectImage { get; set; }
}
public class ProjectImage : BaseEntity
{
public string Description { get; set }
public Guid ThumbnailGuid { get; set; }
public Guid FullSizeImageGuid { get; set; }
[ForeignKey("ImageFileGuid")]
public ImageFile Thumbnail { get; set; }
[ForeignKey("ImageFileGuid")]
public ImageFile FullSizeImage { get; set; }
}