1

I keep getting this error when I try to submit to the database:

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

Here's where it breaks:

public static bool Save(NPPR_Provider provider, State state, string filename, bool validateBeforeSave = true )
{
    using (var db = new NPPRContext(state))
    {
        var prov = new NPPR_Provider()
        {
            First = provider.First,
            Middle = provider.Middle,
            Last = provider.Last,
            DateOfBirth = provider.DateOfBirth,
            DateOfDeath = provider.DateOfDeath,
            Gender = provider.Gender,
            SSN = provider.SSN,
            DegreeCode = provider.DegreeCode,
            BusinessName = provider.BusinessName,
            DbaName = provider.DbaName,
            Action = "A",
            EffectiveDate = "20121212",
            EndDate = "99991231",

            NPPR_ServLocation = new NPPR_ServLocation()
            {
                EnrollmentType = provider.NPPR_ServLocation.EnrollmentType,
                OrganizationType = provider.NPPR_ServLocation.OrganizationType,
                ProviderTypeCode = provider.NPPR_ServLocation.ProviderTypeCode,
                IRSTaxAssociations = provider.NPPR_ServLocation.IRSTaxAssociations,
                NPIAssociations = provider.NPPR_ServLocation.NPIAssociations,
                Address = provider.NPPR_ServLocation.Address
            },
            NPPR_Header = new NPPR_Header()
            {
                FileName = filename,
                TransactionDate = Utilities.DateTimeToPRNDate(DateTime.Now),
                FileLoadDate = DateTime.Now,
                SubmitterId = "M00000503",
                Purpose = "A",
                Action = "A"
            }
        };
        foreach(var npi in prov.NPPR_ServLocation.NPIAssociations)
        {
            npi.NPIType = prov.NPPR_ServLocation.OrganizationType == "I" ? "1" : "2";
        }

        prov.NPPR_ServLocation.Licenses = SegmentOrNull<NPPR_Licenses>(provider.NPPR_ServLocation, "Licenses", "LicenseNumber");
        prov.NPPR_ServLocation.Certifications = SegmentOrNull<NPPR_Certifications>(provider.NPPR_ServLocation, "Certifications", "CertificationNumber");
        prov.NPPR_ServLocation.Specialties = SegmentOrNull<NPPR_Specialties>(provider.NPPR_ServLocation, "Specialties", "SpecialtyCode");
        prov.NPPR_ServLocation.Taxonomies = SegmentOrNull<NPPR_Taxonomies>(provider.NPPR_ServLocation, "Taxonomies", "TaxonomyCode");
        prov.NPPR_ServLocation.OtherIds = SegmentOrNull<NPPR_OtherIds>(provider.NPPR_ServLocation, "OtherIds", "IdentifierTypeId");
        prov.NPPR_ServLocation.GroupAssociations = SegmentOrNull<NPPR_GroupAssociations>(provider.NPPR_ServLocation, "GroupAssociations", "ProviderLocationId");
        db.NPPR_Provider.Add(prov);
        if (validateBeforeSave)
            db.SaveChangesWithValidation();
        else
            db.SaveChanges();
        return true;
    }
}

db.SaveChanges() is where, according to the stacktrace, the exception is thrown. This function was originally written for EF Core, but due to issues with the server, I was forced to turn everything to EF6.4. Under Core, this method worked fine, but under EF6, it throws an exception. I've tried a few things I've read on other SO questions but so far no luck.

Specifically the exception throws on the NPPR_Provider primary key, NPPRProvId, which at no point in my code is ever read or written, except to be defined in the model.

In SQL, the NPPRProvId is PK, int, not null

The model involved:

public class NPPR_Provider : Provider
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int NPPRProvId { get; set; }

    //[ForeignKey("NPPR_Header")]
    public int? NPPRFileId { get; set; }

    [ForeignKey("NPPRFileId")]
    public NPPR_Header NPPR_Header { get; set; }

    [ForeignKey("NPPRProvId")]
    public NPPR_ServLocation NPPR_ServLocation { get; set; }
}

What am I doing wrong here and how might I fix it? I'm still fairly new to EF and this is my first major project in EF and MVC, and in the .NET framework in general.

cokeman19
  • 2,405
  • 1
  • 25
  • 40
Jonathan Kuhl
  • 699
  • 9
  • 23
  • 1
    The reason of your error is you defined a bad relationship between `NPPR_Provider` and `NPPR_ServLocation` see these ([1](https://stackoverflow.com/questions/6384659/a-dependent-property-in-a-referentialconstraint-is-mapped-to-a-store-generated-c),[2](https://entityframework.net/knowledge-base/50011305/entity-framework---one-to-one---referentialconstraint-is-mapped-to-a-store-generated-column)) links – Eldar Dec 15 '19 at 18:55

1 Answers1

0

In EF Core the NPPR_ServLocation could be an owned object but to my knowledge in EF 6 there are not owned objects; you need to define them explicitly in a separate table and give them keys.

public class NPPR_Provider : Provider
{
    public int NPPR_ProviderId { get; set; }

    public NPPR_Header NPPR_Header { get; set; }
    public NPPR_ServLocation NPPR_ServLocation { get; set; }

    … // data properties here
}

public class NPPR_Header {
    public int NPPR_HeaderId {get;set;}

    public int NPPR_ProviderId {get;set;}
    public NPPR_Provider {get;set;}

    … // data properties here
}

public class NPPR_ServLocation { 
    public int NPPR_ServLocationId {get;set;}

    public int NPPR_ProviderId {get;set;}
    public NPPR_Provider {get;set;}

    … // data properties here.
}
sjb-sjb
  • 1,112
  • 6
  • 14
  • I don't think that works because NPPR_Provider has no foreign key to ServLocation, rather NPPR_ServLocation has a foreign key to NPPR_Provider and that key, NPPRProvId, is the PK for NPPR_Provider. It is a one-to-one relationship – Jonathan Kuhl Dec 20 '19 at 16:55
  • The difference between a 1-1 relationship and a 1-many in EF is that for the 1-1 the principal entity's navigation property is a reference instead of a collection. Recognizing that you are using EF 6 I would still recommend the explanation in the section "Other Relationship Patterns" in https://learn.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api – sjb-sjb Dec 21 '19 at 14:15
  • NOTE: You are not using conventional key names, it will make your life easier if you do. For example NPPR_ProviderId instead of NPPRProvId. – sjb-sjb Dec 21 '19 at 14:17
  • One other point, given that this problem cropped up on migrating down to EF 6 from EF Core. EF Core and EF 6 differ in how related entities are implicitly attached to the context. This is explained well in https://learn.microsoft.com/en-us/archive/msdn-magazine/2016/august/data-points-ef-core-change-tracking-behavior-unchanged-modified-and-added . – sjb-sjb Dec 21 '19 at 14:21
  • EF Core and EF 6 also differ in how temporary database id's are handled (i.e. negative id's used before the entity is saved). In recent versions of EF Core does not assign the temporary id, while earlier it did. See https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#tkv – sjb-sjb Dec 21 '19 at 14:22