1

I'm getting the error:

{"A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'FeeID'."}

The setup: I've created the database by hand (SQL Server 2012 and SSMS)

I do NOT have an edmx file

I have two classes, FeeMetaData and Fee, which map to two tables in the database (PFD.FeeMetaData and PFD.Fees)

Database Structure

FeeMetadata
------------ 
FeeID  BIGINT  IDENTITY(1,1) PRIMARY KEY 
Something VARCHAR(25) NOT NULL

Fees
------------ 
FeeID  BIGINT  PRIMARY KEY NOT NULL
FeeBatchID  BIGINT NOT NULL
PersonID BIGINT 
Amount DECIMAL(18,2) NOT NULL
DueDate DATE NOT NULL

There is a 1-to-1 relationship between FeeMetadata.FeeID and Fees.FeeID

Class Structure

Imports System.ComponentModel.DataAnnotations
Imports System.ComponentModel.DataAnnotations.Schema
Namespace PFD
    <Table("FeeMetadata", Schema:="PFD")>
    Public Class FeeMetadata

        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal tFee As SOACourt_v1)
            Me.New()
            Me.PfdFee = New PFD.Fee(tFee)
        End Sub

        <Key>
        <DatabaseGenerated(DatabaseGeneratedOption.Identity)>
        Public Property FeeID As Int64

        Public Property Something As String

        <ForeignKey("FeeID")>
        Public Property PfdFee As PFD.Fee
    End Class
End Namespace





Namespace PFD
<Table("Fees", Schema:="PFD")>
Public Class Fee
    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub New(ByVal tFee As SOACourt_v1)
        Me.New()
        Me.Amount = tFee.Amount
        Me.DueDate = tFee.DueDate
    End Sub


    <DatabaseGenerated(DatabaseGeneratedOption.None)>
    Public Property FeeID As Int64

    Public Property FeeBatchID As Int64 = 0

    Public Property PersonID As Int64? = 0

    Public Property Amount As Decimal

    Public Property DueDate As Date = Date.Today
End Class
End Namespace

Usage

Using tContext As FeesContext = New FeesContext
    For Each tFee As SOACourt_v1 In tFees
        tContext.FeeMetadata.Add(New PFD.FeeMetadata(tFee))
    Next
    tContext.SaveChanges()     '  <---- Error occurs here
End Using

Any Ideas on what is causing the error:

{"A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'FeeID'."}

Charles
  • 50,943
  • 13
  • 104
  • 142
Sam Axe
  • 33,313
  • 9
  • 55
  • 89
  • I had a similar problem, though using Database-first. Our database had a number of tables with IDENTITY columns for primary keys. In the mode, we changed these to use an inheritance structure instead. Although we removed the primary keys from the model, there were still parts of the .EDMX file that thought those columns were still IDENTITY columns. I had to manually edit the file to fix that. – John Saunders Dec 21 '12 at 01:37
  • I'm glad you got that fixed John! Nice to hear success stories. :) – Sam Axe Dec 21 '12 at 01:40
  • Just in case its too burried in the text.. I am not using an edmx file. – Sam Axe Dec 21 '12 at 01:40
  • I noticed that. I felt there could still be something similar. For instance, are you sure that the foreign key isn't referring to the correct table, or that you have "Identity" on the correct table? – John Saunders Dec 21 '12 at 01:43

1 Answers1

5

Although I haven't used EF code-first yet, this looks like the same error you get if you set up your entity relationships incorrectly in a model diagram. In particular, it looks to me like you have your foreign key set up backwards.

The error is because you told Entity Framework to use the FeeId field as the foreign key between FeeMetaData and Fee, but that field is auto-generated in the FeeMetaData class. That's almost certainly not what you intended to do.

If Fee is the primary table and FeeMetaData has the foreign key, then you should put the identity field in Fee. If the tables are the other way around, then your classes are backwards and you should instead define Fee to have a FeeMetaData property with FeeId as the foreign key.

Michael Edenfield
  • 28,070
  • 4
  • 86
  • 117
  • I posted a follow-on question here if you care to take a look: http://stackoverflow.com/questions/13995858/entity-framework-1-1-relationship-is-not-persisting-data-to-the-database – Sam Axe Dec 21 '12 at 19:15