MapleStory MapleStory - 2 months ago 16
ASP.NET (C#) Question

EF:“A dependent property in a ReferentialConstraint is mapped to a store-generated column” with Id change

I happened with this error as title said.I search the google and can not find answer,because if i use sql to insert ,it is ok,and when I use the ef to add ,it can not work.looks at below,it is my database schema.
enter image description here

when I try to Add something to good table ,the error occured.the fluent api of good seems that like this:

using ppdai.user.vip.model;
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Configuration;

namespace ppdai.user.vip.model.mapping
{
public class GoodsMapping: EntityTypeConfiguration<Goods>
{
public GoodsMapping()
{
this.Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.HasKey(x => x.Id);


this.Property(x => x.GoodsQuantity).IsRequired();
this.Property(x => x.GoodPrice).IsRequired();
this.Property(x => x.GoodScore).IsRequired();
this.Property(x => x.GoodTypeId).IsRequired();

this.HasRequired(x => x.GoodType)
.WithMany(x => x.Goods)
.HasForeignKey(x => x.GoodTypeId);


}

}
}


as a result ,good is the position of "center" table in my database ,anyone know how to solve it?

Answer

It looks like one of your foreign key columns is set to be Database Generated. Check that GoodTypeId in Goods is not set as auto incremented Identity. Also check the same for GoodId in OrderItems and GoodId in HomePageProducts.