好吧,這是情況..我遇到了一個奇怪的異常,並通過各種其他答案在這裏stackoverflow我已經發現它必須有一些與我的項目之間的映射 - 實體和與圖像實體的一對一關係。儘管如此,我仍然無法弄清楚錯誤的確切原因是什麼。異常 - ReferentialConstraint中的依賴屬性映射到商店生成的列。列:'Id'
我讀過到目前爲止這些問題的答案,但沒有人幫我解決不幸的錯誤。我想這一定是一些非常小的和愚蠢。。
- A dependent property in a ReferentialConstraint is mapped to a store-generated column - when saving object
- A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column:
- A dependent property in a ReferentialConstraint is mapped to a store-generated column error on 1-to-1 relationship
這是確切的錯誤我得到
我的整個模型是使用代碼優先遷移創建的,最近我從數據註釋更改爲流暢的映射。我已經讓MVC支架基於我的DTO創建了一個簡單的索引,創建和編輯視圖。我的想法是,我可以創建一個與至少一個「投資組合」和一個或多個圖像相關的項目。
Project.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; }
}
Portfolio.cs
public class Portfolio : BaseEntity
{
public String Name { get; set; }
public virtual List<Project> Projects { get; set; }
}
Image.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; }
}
個ImageContent.cs
public class ImageContent
{
//[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public byte[] Content { get; set; }
}
這是所使用的所有實體對象。我使用automapper將屬性從DTO映射到實體對象。但是你可以假設所有DTO的屬性與實體對象中的屬性完全相同。
然後,使用以下在上下文中加載的類映射實體對象。
ProjectMap.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
}
}
ImageContentMap。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'
有人可以幫我解決這個問題嗎?
我看到你註釋掉了//[DatabaseGenerated(DatabaseGeneratedOption.Identity)]。任何機會EF都沒有清理它? http://stackoverflow.com/questions/6384659/a-dependent-property-in-a-referentialconstraint-is-mapped-to-a-store-generated-c – 2015-03-30 18:55:53
嘗試更改'公共Guid SmallImageId {get;組; ''到'公共Guid? SmallImageId {get;組; }'在'Project'中 – SWilko 2015-03-30 21:11:48
@SteveGreene值得一試,儘管我已經刪除並重新創建了整個數據庫 – Rob 2015-03-31 06:16:43