0

I am having some trouble with one to one relationships in EF.

Normally, I would define the key side of a (one to many) relationship, like so:

[ForeignKey("OrderId")]
public virtual Order Order { get; set; }

But when I do this with a one to one relationship, it fails to determine the principal end...

Unable to determine the principal end of an association between the types 'Retailer.ClientDB.EF.OrderOrigin' and 'Retailer.ClientDB.EF.Order'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

Strangely, I don't get the error if I put the ForeignKeyAttribute on the id column, as I have done here with the relationship between Order and OrderOrigin.

[Table("ORDER_ORIGIN")]
public class OrderOrigin
{
    [Key, Column("uidorder"), ForeignKey("Order")]
    public int OrderId { get; set; }

    [Column("importfilename")]
    public string ImportFileName { get; set; }

    public virtual Order Order { get; set; }
}


[Table("ORDERS")]
public class Order
{
    [Column("uidorder")]
    public int Id { get; set; }

    [Column("uidstatus")]
    public int OrderStatus { get; set; }

    public virtual OrderOrigin OrderOrigin { get; set; }
}

But now, when I attempt to include() like this (which would normally work great)...

public List<ClientDB.EF.OrderOrigin> ListOrderOrigin_WithOrder(Common.Data.DBContext context)
{
   return context.EFDatabase.OrderOrigin.Include("Order").ToList();
}

...it fails to include Order and errors with the following when I try and access Order (after the context has been disposed of):

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

Can anyone please advise how I can code one to one relationships in order to get this to work?

Thanks

Masoud
  • 8,020
  • 12
  • 62
  • 123
Beakie
  • 1,948
  • 3
  • 20
  • 46
  • The ObjectDisposedException has nothing to do with the fact that you are including the navigation property, you are passing the method a context that has been disposed. This usually happens when you have a method that returns a context that is in a `using` block. – Ben Robinson Oct 01 '14 at 12:18
  • @BenRobinson Actually, I am not. This method executes fine. Once it has run, I take the results, dispose of the context and do stuff with the returned data. This is when it errors. Normally, including include() would get the extra data I need and include it in the dataset. This is not happening. – Beakie Oct 01 '14 at 12:20
  • It looks like it can't map your Order FK because there is no key field in the Order table called OrderId. I could be wrong though. – Daniel Lane Oct 01 '14 at 12:30
  • @CapTec The field is called uidorder and when I get orders directly (not via orderorigin) it works fine. The fault is not within the order definition itself, it is in the relationship. – Beakie Oct 01 '14 at 12:33
  • 1
    @Beakie You may wish to read this [Shared primary keys](http://stackoverflow.com/a/10108574/1396796) – Daniel Lane Oct 01 '14 at 12:41
  • @CapTec Thanks for this. Problem solved with recommended chunk of fluent API instead of attributes. (Add as answer and I will mark it as such?) – Beakie Oct 01 '14 at 12:55
  • @Beakie Whilst I'd get rep for posting an answer and you accepting it, I can't take the credit for the original posters work. Cheers though. – Daniel Lane Oct 16 '14 at 12:51

0 Answers0