C# 异常-引用约束中的依赖属性映射到存储生成的列。专栏:';Id';

C# 异常-引用约束中的依赖属性映射到存储生成的列。专栏:';Id';,c#,entity-framework,ef-code-first,ef-fluent-api,C#,Entity Framework,Ef Code First,Ef Fluent Api,好的,情况就是这样。我遇到了一个奇怪的异常,通过stackoverflow上的各种其他答案,我发现它一定与我的项目实体和与图像实体的一对一关系之间的映射有关。尽管如此,我仍然无法找出错误的确切原因 到目前为止,我已经阅读了这些答案,但不幸的是,它们都没有帮助我解决这个错误。。我猜一定是很小很愚蠢的东西 这正是我得到的错误 我的整个模型是通过代码优先迁移创建的,最近我从数据注释改为流畅的映射。我让MVC基于我的DTO构建了一个简单的索引、创建和编辑视图。我的想法是,我可以创建一个项目,

好的,情况就是这样。我遇到了一个奇怪的异常,通过stackoverflow上的各种其他答案,我发现它一定与我的项目实体和与图像实体的一对一关系之间的映射有关。尽管如此,我仍然无法找出错误的确切原因

到目前为止,我已经阅读了这些答案,但不幸的是,它们都没有帮助我解决这个错误。。我猜一定是很小很愚蠢的东西

这正是我得到的错误

我的整个模型是通过代码优先迁移创建的,最近我从数据注释改为流畅的映射。我让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; }
}
public class Portfolio : BaseEntity
{
    public String Name { get; set; }

    public virtual List<Project> Projects { get; set; }
}
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; }
}
public class ImageContent
{
    //[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    public byte[] Content { get; set; }
}
 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
    }
}
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
    }
}
 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
    }
}
 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
    }
}
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();
    }
}
Portfolio.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; }
}
public class Portfolio : BaseEntity
{
    public String Name { get; set; }

    public virtual List<Project> Projects { get; set; }
}
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; }
}
public class ImageContent
{
    //[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    public byte[] Content { get; set; }
}
 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
    }
}
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
    }
}
 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
    }
}
 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
    }
}
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();
    }
}
ImageContent.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; }
}
public class Portfolio : BaseEntity
{
    public String Name { get; set; }

    public virtual List<Project> Projects { get; set; }
}
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; }
}
public class ImageContent
{
    //[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    public byte[] Content { get; set; }
}
 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
    }
}
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
    }
}
 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
    }
}
 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
    }
}
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();
    }
}
这是所有使用的实体对象。我使用automapper将属性从DTO映射到实体对象。但您可以假设所有DTO的属性与实体对象中的属性完全相同

然后使用以下类映射entityobjects,这些类在上下文中加载

ProjectMap.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; }
}
public class Portfolio : BaseEntity
{
    public String Name { get; set; }

    public virtual List<Project> Projects { get; set; }
}
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; }
}
public class ImageContent
{
    //[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    public byte[] Content { get; set; }
}
 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
    }
}
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
    }
}
 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
    }
}
 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
    }
}
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();
    }
}

有人能帮我解决这个问题吗?

终于解决了这个问题!我的一个同事给了我一个提示,关于映射到正确的id。所以我检查了project.cs的所有关系

项目属于称为公文包的项目。这背后的想法是,您可以创建一个或多个包含项目的投资组合。portfolio类的映射如下所示

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(); <<--- Line causing the error

        //Other constraints
    }
}
public类PortfolioMap:BaseEntityTypeConfiguration
{
公共PortfolioMap()
{
//主键
HasKey(t=>t.Id);
//映射架构名称和表
ToTable(“投资组合”,SchemaConstants.Component);
//如果需要,将属性映射设置为显式
属性(t=>t.Name).HasMaxLength(150).IsRequired();
//外键关系

HasRequired(t=>t.Projects)。WithRequiredPrincipal();我看到您有一个注释掉的//[DatabaseGenerated(DatabaseGenerateOption.Identity)]。EF有没有可能没有清除它?请尝试将
public Guid SmallImageId{get;set;}
更改为
public Guid?SmallImageId{get;set;}
项目中
@SteveGreene值得一试,尽管我已经删除并重新创建了整个项目database@dellywheel这难道不会使“SmallImage”属性成为可选的吗?因为这是我不想要的。