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 Order
StatusId 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.
<EdmEntityTypeAttribute(NamespaceName:="OrderModel", Name:="Order")>
<Serializable()>
<DataContractAttribute(IsReference:=True)>
Public Partial Class Order
Inherits EntityObject
<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
<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
<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
<EdmEntityTypeAttribute(NamespaceName:="OrderModel", Name:="Status")>
<Serializable()>
<DataContractAttribute(IsReference:=True)>
Public Partial Class Status
Inherits EntityObject
<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
<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:
<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.