0

I am using EF 4 code first, and I am having a heck of a time here. I keep getting the error:

{"Introducing FOREIGN KEY constraint 'SalesRepresentative_SalesOrders' on table 'SalesOrders' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint. See previous errors."}

Consider the following code. If I comment out the Foreign Key ID fields, it generates its own and it works, but if I don't then I get the error.

Public Class SalesOrder
        Inherits EntityBase(Of SalesOrder)

#Region "Members/Properties"

        Public Property ID As Integer
        'Public Property CustomerID As Integer
        'Public Property CustomerLocationID As Integer
        'Public Property SalesRepresentativeID As Integer
        'Public Property SalesOrderStatusID As Integer

        Public Overridable Property Customer As Customer
        Public Overridable Property CustomerLocation As CustomerLocation
        Public Overridable Property Items As ICollection(Of SalesOrderItem)
        Public Overridable Property Status As SalesOrderStatus
        Public Overridable Property SalesRepresentative As SalesRepresentative

#End Region

    End Class

Public Class SalesRepresentative
        Inherits EntityBase(Of SalesRepresentative)

#Region "Members/Properties"

        Public Property ID As Integer

        Public Property FirstName As String
        Public Property LastName As String

        Public Overridable Property Customers As ICollection(Of Customer)
        Public Overridable Property SalesOrders As ICollection(Of SalesOrder)

#End Region

    End Class

So I am wondering a few things:

Do I have to create the Foreign Key property and the navigation property? Do I just create the navigation property on the child object? Do I just create the navigation property on the parent object?

Anyone have any ideas? Thanks!!

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
Sam
  • 15,336
  • 25
  • 85
  • 148

1 Answers1

1

You don't have to create navigation property on both sides but you must create it at least on one side to have relation created in the database.

Also you don't have to use property for foreign key. This property differs between independent and foreign key association. Just say that using foreign key property is against ORM idea but it makes live with EF much easier.

The part with commenting FK is interesting. Can you validate in the database that relations are created and how are they configured (cascading deletes)?

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • I found that if I do not define the FK, the EF creates it in the database, but it is nullable. So that kinda sucks!! So I am thinking in order to get exactly what you want, you need to define the key and the nav property on the child end of the relationship, and then the nav property on the parent object? At least the nav property on the parent and the FK on the child? – Sam Apr 04 '11 at 06:00
  • Yes it does create the relationship when I comment out the properties, but the FK are nullable. – Sam Apr 04 '11 at 06:01
  • You don't need nav. properties on both sides. In fluent mapping you can either use `.HasOptional` or `.HasRequired` to define if the nav. property on FK side is nullable or not. – Ladislav Mrnka Apr 04 '11 at 06:04