Entity Framework: Setting a Foreign Key Property
Asked Answered
O

6

16

We have a table that looks roughly like this:

CREATE TABLE Lockers 
{
  UserID int NOT NULL PRIMARY KEY (foreign key),
  LockerStyleID int (foreign key),
  NameplateID int (foreign key)
}

All of the keys relate to other tables, but because of the way the application is distributed, it's easier for us to pass along IDs as parameters. So we'd like to do this:

Locker l = new Locker { 
  UserID = userID, 
  LockerStyleID = lockerStyleID, 
  NameplateID = nameplateID 
};
entities.AddLocker(l);

We could do it in LINQ-to-SQL, but not EF?

Ossified answered 26/1, 2009 at 18:34 Comment(3)
This is something I'd like to find out too. From what I read you have to give it the entity via the Navigation links.Mahican
If I could, I would retag this question a little bit. It is specific to .NET 3.5, MS will fix the issue with .NET 4 (or so I read somewhere...)Amby
They got LINQtoSQL so right and LINQtoEntities so wrong...SConfessor
A
16

This missing feature seems to annoy a lot of people.

  • Good news: MS will address the issue with .NET 4.0.
  • Bad news: for now, or if you're stuck on 3.5 you have to do a little bit of work, but it IS possible.

You have to do it like this:

Locker locker = new Locker();
locker.UserReference.EntityKey = new System.Data.EntityKey("entities.User", "ID", userID);
locker.LockerStyleReference.EntityKey = new EntityKey("entities.LockerStyle", "ID", lockerStyleID);
locker.NameplateReference.EntityKey = new EntityKey("entities.Nameplate", "ID", nameplateID);
entities.AddLocker(locker);
entities.SaveChanges();
Amby answered 24/6, 2009 at 11:59 Comment(3)
Yes, that missing feature is annoying! :)Ossified
If i need set a null a previous relationship?Postern
I just found this old question. Do you happen to know if there's an updated way to do this?Freemon
T
3

What I've been doing to make things easy is adding the foreign key property myself in the partial class:

public int UserID
{
   get
   {
      if (this.User != null)
         return this.User.UserID;
   }
   set 
   {
      this.UserReference.EntityKey = new System.Data.EntityKey("entities.User", "ID", value);
   }
}
Thymus answered 14/10, 2009 at 19:7 Comment(0)
R
0

You could make an extension method that constructs the entity based on these ID's.

Reopen answered 26/1, 2009 at 18:43 Comment(0)
V
0

Using an EntityKey solves your problem ;)

alk.

Vladimar answered 18/2, 2009 at 10:36 Comment(0)
C
0

another method if you don't mind 'polluting' you db schema is to add a computed column, e.g. if you had a foreign key field FK_Customer you could define a new computed column FK_Customer_Computed which has the expression FK_Customer. When you generate\update your edmx model the field will appear like a regular field that you can then reference from you entity object.

Or wait for EF4 :)

Cambodia answered 20/2, 2010 at 14:1 Comment(0)
M
0

Following on from Dylan's answer, Alex James has written a blog on precisely this, explaining the problem in full and how to go about the partial class + property solution.

Faking Foreign Keys - EF3.5

Maxama answered 20/3, 2014 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.