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

后端 未结 13 1499
终归单人心
终归单人心 2020-11-27 17:23

I get this error when writing to the database:

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

13条回答
  •  醉话见心
    2020-11-27 17:53

    My problem was caused by redundant defining of the Primary key in the configuration.

    this
       .Property(p => p.Id)
       .HasColumnName(@"id")
       .IsRequired()
       .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity) // this is redundant when you want to configure a One-to-Zero-or-One relationship
       .HasColumnType("int");
    

    Remove this line

    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)


    Example http://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx

    This is enough to define the relationship

    // Configure Student & StudentAddress entity
    modelBuilder.Entity()
                .HasOptional(s => s.Address) // Mark Address property optional in Student entity
                .WithRequired(ad => ad.Student); // mark Student property as required in StudentAddress entity. Cannot save StudentAddress without Student
    

提交回复
热议问题