0

Okay, this is the situation..I'm running into a strange exception, and trough various other answers here on stackoverflow I've figured out that it must have something to do with the mapping between my Project-entity and the one-to-one relation to an Image-entity. Nevertheless I still can't figure out what the exact cause of the error is.

I've read these answers so far, but none of them helped me solving the error unfortunately.. I guess it must be something very small and stupid..

This is the exact error i'm getting enter image description here

My entire model is created with code first migrations and I've recently changed from data-annotions to fluent mapping. I've let MVC scaffold an simple Index-, Create- and Edit-view based on my DTO's. And the idea is that I can create a project which is related to atleast one 'Portfolio' and one or more images.

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; }
}

This are all the entity objects used. I'm using automapper to map the properties from the DTO's to the entity objects. But you can assume that the properties of all the DTO's are exactly the same as those in the entity objects.

The entityobjects are then mapped using the following classes, which are loaded in the context.

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();
    }
}

Hopefully somebody can help me resolving this issue. Right now this is driving me nuts! For other objects saving an new item works fine. But when creating a new project through the interface all i'm getting is a DbUpdateException with this error;

A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'Id'

Can somebody help me out on this?

Community
  • 1
  • 1
Rob
  • 6,731
  • 12
  • 52
  • 90
  • I see you have a commented out //[DatabaseGenerated(DatabaseGeneratedOption.Identity)]. Any chance EF didn't clean that up? http://stackoverflow.com/questions/6384659/a-dependent-property-in-a-referentialconstraint-is-mapped-to-a-store-generated-c – Steve Greene Mar 30 '15 at 18:55
  • try changing `public Guid SmallImageId { get; set; }` to `public Guid? SmallImageId { get; set; }` in `Project` – SWilko Mar 30 '15 at 21:11
  • @SteveGreene Is worth a try, though i've already dropped and re-created the entire database – Rob Mar 31 '15 at 06:16
  • @dellywheel Wouldn't that make the 'SmallImage'-property optional? Because that is what I don't want. – Rob Mar 31 '15 at 06:18

1 Answers1

0

Finally solved the problem! A colleague of mine gave me a hint, about mapping to the proper id's. So i've checked all relations around project.cs

A project belongs to an item called a portfolio. The idea behind this is that you can create one or more portfolio's with projects in it. The mapping for the portfolio class looked like this;

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
    }
}

Removing the strange HasRequired solved the issue.

Rob
  • 6,731
  • 12
  • 52
  • 90