好的,情况就是这样......我遇到了一个奇怪的异常,并且通过stackoverflow上的各种其他答案我发现它必须与我的项目实体和一对一之间的映射有关-与图像实体的一种关系。尽管如此,我仍然无法弄清楚错误的确切原因是什么。
到目前为止,我已经阅读了这些答案,但不幸的是,它们都没有帮助我解决错误。我想它一定是非常小而且愚蠢的东西。
- ReferentialConstraint 中的依赖属性映射到存储生成的列 - 保存对象时
- ReferentialConstraint 中的依赖属性映射到存储生成的列。柱子:
- ReferentialConstraint 中的依赖属性映射到存储生成的 1 对 1 关系列错误
这是我得到的确切错误
我的整个模型是使用代码优先迁移创建的,我最近从数据注释更改为流畅的映射。我让 MVC 基于我的 DTO 构建了一个简单的索引、创建和编辑视图。这个想法是我可以创建一个与至少一个“投资组合”和一个或多个图像相关的项目。
项目.cs
public class Project : BaseEntity
{
public DateTime StartDate { get; set; }
public DateTime? EndDate { get; set; }
public String Title { get; set; }
public String ShortDescription { get; set; }
public String Description { get; set; }
public Guid? LargeImageId { get; set; }
public Image LargeImage { get; set; }
public Guid? MediumImageId { get; set; }
public Image MediumImage { get; set; }
public Guid SmallImageId { get; set; }
public Image SmallImage { get; set; }
public Guid PortfolioId { get; set; }
public virtual Portfolio Portfolio { get; set; }
}
投资组合.cs
public class Portfolio : BaseEntity
{
public String Name { get; set; }
public virtual List<Project> Projects { get; set; }
}
图片.cs
public class Image : BaseEntity
{
public Int32 Width { get; set; }
public Int32 Height { get; set; }
public String Title { get; set; }
public String MimeType { get; set; }
public String Extension { get; set; }
public String Filename { get; set; }
public Guid FileContentId { get; set; }
public virtual ImageContent FileContent { get; set; }
}
图像内容.cs
public class ImageContent
{
//[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public byte[] Content { get; set; }
}
这是使用的所有实体对象。我正在使用 automapper 将 DTO 的属性映射到实体对象。但是您可以假设所有 DTO 的属性与实体对象中的属性完全相同。
然后使用在上下文中加载的以下类映射实体对象。
项目地图.cs
public class ProjectMap : BaseEntityTypeConfiguration<Project>
{
public ProjectMap()
{
//Primary key
HasKey(t => t.Id);
//Map schema name and table
ToTable("Project", SchemaConstants.Component);
//Set property mapping explicit if needed
Property(t => t.StartDate).IsRequired();
Property(t => t.EndDate).IsOptional();
Property(t => t.Title).HasMaxLength(150).IsRequired();
Property(t => t.ShortDescription).HasMaxLength(200).IsOptional();
Property(t => t.Description).IsRequired().HasColumnType("nvarchar(max)");
//Foreign key relationships
HasRequired(t => t.SmallImage).WithMany().HasForeignKey(t => t.SmallImageId).WillCascadeOnDelete(true);
HasOptional(t => t.MediumImage).WithMany().HasForeignKey(t => t.MediumImageId).WillCascadeOnDelete(false);
HasOptional(t => t.LargeImage).WithMany().HasForeignKey(t => t.LargeImageId).WillCascadeOnDelete(false);
HasRequired(t => t.Portfolio).WithMany().HasForeignKey(t => t.PortfolioId).WillCascadeOnDelete(false);
//Other constraints
}
}
PortfolioMap.cs
public class PortfolioMap : BaseEntityTypeConfiguration<Portfolio>
{
public PortfolioMap()
{
//Primary key
HasKey(t => t.Id);
//Map schema name and table
ToTable("Portfolio", SchemaConstants.Component);
//Set property mapping explicit if needed
Property(t => t.Name).HasMaxLength(150).IsRequired();
//Foreign key relationships
HasRequired(t => t.Projects).WithRequiredPrincipal();
//Other constraints
}
}
ImageMap.cs
public class ImageMap : BaseEntityTypeConfiguration<Image>
{
public ImageMap()
{
//Primary key
HasKey(t => t.Id);
//Map schema name and table
ToTable("Image", SchemaConstants.Systeem);
//Set property mapping explicit if needed
Property(t => t.Width).IsRequired();
Property(t => t.Height).IsRequired();
Property(t => t.Title).HasMaxLength(150).IsRequired();
Property(t => t.MimeType).HasMaxLength(150).IsRequired();
Property(t => t.Extension).HasMaxLength(15).IsRequired();
Property(t => t.Filename).HasMaxLength(255).IsRequired();
//Foreign key relationships
HasRequired(t => t.FileContent).WithMany().HasForeignKey(t => t.FileContentId).WillCascadeOnDelete(true);
//Other constraints
}
}
图像内容映射.cs
public class ImageContentMap : EntityTypeConfiguration<ImageContent>
{
public ImageContentMap()
{
//Primary key
HasKey(t => t.Id);
//Map schema name and table
ToTable("ImageContent", SchemaConstants.Systeem);
//Set property mapping explicit if needed
Property(t => t.Content).IsRequired();
//Foreign key relationships
//Other constraints
}
}
BaseEntityTypeConfiguration.cs
public class BaseEntityTypeConfiguration<T> : EntityTypeConfiguration<T> where T : BaseEntity
{
public BaseEntityTypeConfiguration()
{
Property(t => t.TimeStamp).IsRowVersion().IsConcurrencyToken();
Property(t => t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(t => t.PublishStart).IsRequired();
Property(t => t.PublishEnd).IsOptional();
Property(t => t.DateCreated).IsRequired();
Property(t => t.DateDeleted).IsOptional();
Property(t => t.IsPublished).IsRequired();
Property(t => t.IsDeleted).IsRequired();
Property(t => t.SystemName).HasMaxLength(150).IsRequired();
}
}
希望有人可以帮助我解决这个问题。现在这让我发疯了!对于其他对象,保存新项目可以正常工作。但是当通过界面创建一个新项目时,我得到的只是一个带有此错误的 DbUpdateException;
A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'Id'
有人可以帮我解决这个问题吗?