简体   繁体   中英

EF6 Inserting Composite key record errors

I have an interesting conundrum occurring that i am hoping others will be able to assist in shedding some light upon.

When inserting a new entry using EF6 to a table that has a composite key made up of two Guids where one member is to be assigned from the DB while the other has its value supplied by the application i am getting mixed responses.

For one table the insertion occurs without any issue while for another with the exact same key and identity column setup the insertion fails with a message 'InvalidOperationException: A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'ID'.' Below are the (simplified) classes with their configuration setups

The Class that fails to insert :

public class User
{
    public Guid ID { get; set; }
    public Guid CompanyId { get; set; }
    public Guid OrganisationId { get; set; }
    public String PasswordHash { get; set; }
    public String Salt { get; set; }
    public String UserName { get; set; }

    public virtual Company Company { get; set; }
    public virtual Organisation Organisation { get; set; }

    // <inconsequential members omitted> 
}


public class UserConfiguration : EntityTypeConfiguration<User>
{
    public UserConfiguration()
    {
        HasKey(u => new { u.CompanyId, u.ID });
        Property(u => u.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        Property(u => u.UserName).IsRequired().HasMaxLength(256);
        Property(u => u.PasswordHash).IsRequired().HasMaxLength(256);
        Property(u => u.Salt).IsRequired().HasMaxLength(256);
        HasRequired(c => c.Company).WithMany(c => c.Users).HasForeignKey(c => c.CompanyId).WillCascadeOnDelete(false);
        HasRequired(c => c.Organization).WithMany(c => c.Users).HasForeignKey(p => p.OrganizationID).WillCascadeOnDelete(false);

        // < inconsequential members omitted>
    }
}

The Class that inserts without issue :

public class AdditionalInformation
{
    public Guid ID { get; set; }
    public Guid CompanyId { get; set; }
    public Guid CustomFieldId { get; set; }
    public Guid UserId{ get; set; }

    public virtual Company Company { get; set; }
    public virtual User User { get; set; }

    // <inconsequential members omitted> 
}

public class AdditionalInformationConfiguration : EntityTypeConfiguration<AdditionalInformation>
{
    public AdditionalInformationConfiguration()
    {
        HasKey(ai => new { ai.CompanyId, ai.ID });
        Property(u => u.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        HasOptional(ai => ai.User).WithMany(u => u.UserAdditionalInformations).HasForeignKey(ai => new { ai.CompanyId, ai.UserID });

        // <inconsequential members omitted> 
    }
}

Simple test i have been using :

using (var context = new DataContext())
{
    var user = context.Users.First(f => f.Company.Subdomain.StartsWith("demo"));
    var customField = context.CustomFields.First(f => f.CompanyId == user.CompanyId);

    context.Users.Add(new User() { CompanyId = user.CompanyId, PasswordHash = "hash", Salt = "salt", UserName = "anew@entry.com" });
    context.AdditionalInformations.Add(new UserAdditionalInformation() { CustomFieldID = customField.ID, CompanyId = user.CompanyId, UserID = user.ID });
    context.SaveChanges();
}

As you can see, both classes have an ID member as well as a CompanyId member. They both have the Key defined as { CompanyId, Id } and the DatabaseGeneratedOption.Identity on the ID member.

I am very confused as to why when an instance of the AdditionalInformation class is persisted to the DB through the DbContext, the record is created without issue while the User class throws the 'InvalidOperationException: A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'ID'.' error.

I have managed to fix my own problem but cannot explain how the fix has had the effect it has.

The User object had a navigation property to another table which did not expose the FK column on the User object. Once the inferred column was exposed, the insertion problem went away.

I found this by accident since while trying to understand the problem i noticed that this one FK was not explicitly exposed and so did so, then found that my unit test started working after the change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM