locked
Creating a not-mapped association in EF 4.0 RRS feed

  • Question

  • Hi, everyone!

    I am a newbee in EF and I've got stuck with a problem, I can't solve. Maybe it's a simple problem, but, please help me anyway!

    Here what I need to do:

    I have two tables (Orders and People). Orders contain a column UserID, that can contain a corresponding record in People or cannot (if such user no longer exists). There is no FK between the tables.

    I've created an edmx and mapped these two tables to entities. Now I need to create a request in LINQ that will return a set of Order entities with corresponding People entites if exist or null if no. Something like SELECT * FROM Orders o LEFT JOIN People p on p.ID = o.UserID

    The problem is I don't know how to do it.

    "Include" needs an assotiation, creating manually assotiation needs mapping it, and mapping return error about UserID property beeing mapped twice.

    Are there any other solutions? (No complex types, I need original entity)

     

    Monday, October 18, 2010 12:33 PM

Answers

  • I see no problem in adding referential integrity to your database for this
    case.
    But nonetheless it should be possible to add the association only on the
    model side. Maybe you should lookup how these new foreign key associations
    really are set up:

    http://blogs.msdn.com/b/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx

    SELECT * FROM Orders o LEFT JOIN People p on p.ID = o.UserID

    from o in context.Orders
    join p in context.People on o.UserID equals p.ID
    ...

    or

    from o in context.Orders
    let user = context.People.FirstOrDefault(p => p.ID == o.UserID)
    ...

    /\/\arkus.
    Monday, October 18, 2010 3:16 PM