A Dependent Property in a Referentialconstraint Is Mapped to a Store-Generated Column

A dependent property in a ReferentialConstraint is mapped to a store-generated column

Is it possible that you defined a bad column relation between your tables?

In my case, I had different columns and one was set as autonumeric.

Entity Framework 6 A dependent property in a ReferentialConstraint is mapped to a store-generated column. Error

One-to-One relationships in EF6 must use the same keys. EG Address.CustomerId would have to be its key.

Either allow customers to have multiple addresses in the model, or change the key of Address to be CustomerID.

A dependent property in a ReferentialConstraint is mapped to a store-generated column error on 1-to-1 relationship

Here there are two problems, the obvious one, shown in the exception. When you define a one-to-one relationship, the FK must be also the PK. In this case the PK and FK of both entities is the Id field. The problem shown in the exception is that the FK is database generated. So, if you insert a Child1 with a related Child2, EF has no way to set the FK value of the related Child2 because it's database generated.

The second problem, that has still not arisen, is that a one-to-one relationship is only a theoric thing in a database like SQL Server. If you want to insert Child1 that depends on Child2, you need to insert first Child1, and then the related Child2. That's right, but, ooops, you also have to insert Child2 before inserting Child1, because Child1 depends also on Child2. So, having a pure one to one relationship is not possible.

To solve this problem you need to do two things:

  1. make the relationship a 1-to-(0 or 1). I.e. you must have a principal entity and a dependent entity which can or cannot exist. This will allow you to insert the principal entity, without the dependent entity, because with this configuration you can isnert the principal without the dependent.
  2. the principal PK can be left as database generated, but you have to change the PK on the dependent entity not to be db generated. So, when you insert the dependent entity the PK, which is also the FK, can be freely specified by EF.

Finally, if you think of it, a 1-to-1 relationship usually makes no sense. You can use a single table that holds all the columns in both tables, because whenver a row exists in table A, it must exists in table B and viceversa. So having a single table has the same effect.

However, if you still want to use the 1-to-1 relationship, EF allows you to model it like this:

modelBuilder.Entity<Child1>()
.HasRequired(c1 => c1.Child2)
.WithRequiredPrincipal(c2 => c2.Child1);

Note that, in this case, the EF abstraction takes care to allow you to have a 1-to-1 relationship, even if it cannot exists in the DB. However, it's necessary to specify this relationship using the ModelBuilder because you need to specify a principal and a dependent side. In this case the principal is Child1 and the dependent is Child2. Note that you still have to be careful with the rule for database generated values.

NOTE that this is modelled in the DB with a single FK from Child2 to Child1, and not FK from Child1 to Child2. So, in the DB is a (1)-to(0 or 1) relationship, as explained above

public class Child1
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] // Leave as is
public int Id { get; set; }
...

public class Child2
{
//[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[DatabaseGenerated(DatabaseGeneratedOption.None)] // Not db-generated
//[ForeignKey("Child1")] -- specified in the model builder
public int Id { get; set; }

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

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.

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

In Entity Framework, a one-to-one mapping with a required principal is implemented by giving the dependent a primary key that's also a foreign key to the principal. The dependent copies its primary key from the principal.

In your case, EF wants ProductionInstruction.Id to be a foreign key to Order.Id, and its value should be copied from the Order it belongs to. However, because of the inheritance, ProductionInstruction.Id is an identity column (store-generated), so it can't be set in code.

You either have to remove the inheritance, so ProductionInstruction.Id can be mapped as not store-generated, or change the mapping to optional on both sides. The latter will give ProductionInstruction a separate foreign key to Order.

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

I found the problem. It was my fault. I had my FK_Venue_City relationship set as City.ID -> Venue.ID where what I wanted was City.ID -> Venue.CityID. I made that change in my database then updated the model.

A dependent property in a ReferentialConstraint is mapped to a store-generated column. on a persisted computed column (EntityFramework DB first)

I have made a terrible mistake when implementing the approach, but it worked before. I messed up the constraint FK_NATURAL_USER___USER___TYPEVALIDATION, accidentally.

It should have been build like the FK_JURIDICAL_USER___USER___TYPEVALIDATION constraint.

The EF is able to handle the persisted columns. The problem was it tried to write to the PK of [USER_T] that should not be apart of the constraint at all.

I'm sorry for all people who wasted time on this.

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

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.

A dependent property in a ReferentialConstraint is mapped to a store-generated column - when saving object

The problem was on Visual Studio. By deleting the table from the ´.edmx´ file and re-importing it from the database again, the problem was fixed.

I am using Visual Studio Ultimate 2013, and I have no idea if this problem is particular to this version or if it also affects other versions.

All I know is that I wasted 2 days with this. I honestly hope this answer can help someone else in the future.



Related Topics



Leave a reply



Submit