Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi people,
I'm having a problem with Entity Framework 4.0 in .NET 4.0.

I have the following situation:
There's a table in the database with a forreign key relation to another table.
I have an Order table and an OrderStatus table.
My Order has a field OrderStatusId (tinyint, not null), which references the OrderStatusId field (primary key, no key specification, tinyint) in OrderStatus.
Now to insert or update the Order table I have two stored procedures which I have mapped in my model. The OrderStatusId is not an input parameter to the SP's (although I've also tried that), but the SP calculates the OrderStatusId and returns it in a SELECT statement. In the model I've mapped the field OrderStatusId to the return value.

Now here comes the problem: whenever I call ObjectContext.SaveChanges() I get the following error message: A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: OrderStatusId.

I've looked around on Google, but I could only find that people had their foreign keys set up wrong in the database (for example OrderStatusId to OrderStatusId) or in their model (for example made it a one-to-one relation).
So I've triple checked my keys in both the database and in the model, but everything seems to be in order. Yet I keep getting this error message.

UPDATE:
Here's the relevant (generated) code for my Order and Status classes.
VB
<EdmEntityTypeAttribute(NamespaceName:="OrderModel", Name:="Order")>
<Serializable()>
<DataContractAttribute(IsReference:=True)>
Public Partial Class Order
    Inherits EntityObject
     
	' Other code...
	
    ''' <summary>
    ''' No Metadata Documentation available.
    ''' </summary>
    <EdmScalarPropertyAttribute(EntityKeyProperty:=false, IsNullable:=false)>
    <DataMemberAttribute()>
    Public Property StatusId() As Global.System.Byte
        Get
            Return _StatusId
        End Get
        Set
            OnStatusIdChanging(value)
            ReportPropertyChanging("StatusId")
            _StatusId = StructuralObject.SetValidValue(value)
            ReportPropertyChanged("StatusId")
            OnStatusIdChanged()
        End Set
    End Property

    Private _StatusId As Global.System.Byte
    Private Partial Sub OnStatusIdChanging(value As Global.System.Byte)
    End Sub

    Private Partial Sub OnStatusIdChanged()
    End Sub

	' Other code...
	
    ''' <summary>
    ''' No Metadata Documentation available.
    ''' </summary>
    <XmlIgnoreAttribute()>
    <SoapIgnoreAttribute()>
    <DataMemberAttribute()>
    <EdmRelationshipNavigationPropertyAttribute("OrderModel", "FK_Order_Status", "Statu")>
    Public Property Status() As Status
        Get
            Return CType(Me, IEntityWithRelationships).RelationshipManager.GetRelatedReference(Of Status)("OrderModel.FK_Order_Status", "Statu").Value
        End Get
        Set
            CType(Me, IEntityWithRelationships).RelationshipManager.GetRelatedReference(Of Status)("ColdStoreWarehouseModel.FK_Order_Status", "Statu").Value = value
        End Set
    End Property
    ''' <summary>
    ''' No Metadata Documentation available.
    ''' </summary>
    <BrowsableAttribute(False)>
    <DataMemberAttribute()>
    Public Property StatusReference() As EntityReference(Of Status)
        Get
            Return CType(Me, IEntityWithRelationships).RelationshipManager.GetRelatedReference(Of Status)("OrderModel.FK_Order_Status", "Statu")
        End Get
        Set
            If (Not value Is Nothing)
                CType(Me, IEntityWithRelationships).RelationshipManager.InitializeRelatedReference(Of Status)("OrderModel.FK_Order_Status", "Statu", value)
            End If
        End Set
    End Property

    #End Region

End Class

''' <summary>
''' No Metadata Documentation available.
''' </summary>
<EdmEntityTypeAttribute(NamespaceName:="OrderModel", Name:="Status")>
<Serializable()>
<DataContractAttribute(IsReference:=True)>
Public Partial Class Status
    Inherits EntityObject
   
    ' Other code...
   
    ''' <summary>
    ''' No Metadata Documentation available.
    ''' </summary>
    <EdmScalarPropertyAttribute(EntityKeyProperty:=true, IsNullable:=false)>
    <DataMemberAttribute()>
    Public Property StatusId() As Global.System.Byte
        Get
            Return _StatusId
        End Get
        Set
            If (_StatusId <> Value) Then
                OnStatusIdChanging(value)
                ReportPropertyChanging("StatusId")
                _StatusId = StructuralObject.SetValidValue(value)
                ReportPropertyChanged("StatusId")
                OnStatusIdChanged()
            End If
        End Set
    End Property

    Private _StatusId As Global.System.Byte
    Private Partial Sub OnStatusIdChanging(value As Global.System.Byte)
    End Sub

    Private Partial Sub OnStatusIdChanged()
    End Sub

	' Other code...
	
    ''' <summary>
    ''' No Metadata Documentation available.
    ''' </summary>
    <XmlIgnoreAttribute()>
    <SoapIgnoreAttribute()>
    <DataMemberAttribute()>
    <EdmRelationshipNavigationPropertyAttribute("OrderModel", "FK_Order_Status", "Order")>
     Public Property Orders() As EntityCollection(Of Order)
        Get
            Return CType(Me,IEntityWithRelationships).RelationshipManager.GetRelatedCollection(Of Order)("OrderModel.FK_Order_Status", "Order")
        End Get
        Set
            If (Not value Is Nothing)
                CType(Me, IEntityWithRelationships).RelationshipManager.InitializeRelatedCollection(Of Order)("OrderModel.FK_Order_Status", "Order", value)
            End If
        End Set
    End Property

    #End Region

End Class

And the AssociationSet from my edmx file:
XML
<AssociationSet Name="FK_Order_Status" Association="OrderModel.Store.FK_Order_Status">
  <End Role="Status" EntitySet="Status" />
  <End Role="Order" EntitySet="Order" />
</AssociationSet>

<Association Name="FK_Order_Status">
  <End Role="EdiStatus" Type="OrderModel.Store.Status" Multiplicity="1" />
  <End Role="Order" Type="OrderModel.Store.Order" Multiplicity="*" />
  <ReferentialConstraint>
    <Principal Role="Status">
      <PropertyRef Name="StatusId" />
    </Principal>
    <Dependent Role="Order">
      <PropertyRef Name="StatusId" />
    </Dependent>
  </ReferentialConstraint>
</Association>

I've changed the names for simplicity.

Any idea's?
Thanks.
Posted
Updated 12-Nov-14 23:05pm
v2
Comments
Kornfeld Eliyahu Peter 12-Nov-14 14:31pm    
Is it possible that the FK is identity column?
Sander Rossel 12-Nov-14 14:43pm    
Nope, it's not.
Kornfeld Eliyahu Peter 12-Nov-14 14:34pm    
Do you care to share the code for the two tables, the DbContext and for the model corresponding to?
"two heads are better than one"...
Sander Rossel 12-Nov-14 14:43pm    
I'll see what I can do when I'm at the office tomorrow (it's a problem at work).
Sander Rossel 13-Nov-14 5:06am    
Added the generated code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900