Pablo Fernandez
Pablo Fernandez

Reputation: 287400

How to set an Id in Entity Framework

When I have an entity object, how do I set the relationship without having the object, just by having the id? Something like:

Post.BlogId = 42

Upvotes: 2

Views: 734

Answers (2)

Craig Stuntz
Craig Stuntz

Reputation: 126547

Marc is right for EF 4 (+1). But for EF 1 you can do:

Post.BlogReference.EntityKey = 
    new EntityKey("MyEntities.Blogs", "BlogId", 42);

Replace the strings with whatever is right for your model.

Upvotes: 4

marc_s
marc_s

Reputation: 754408

You wait until Entity Framework v4 ships with .NET 4.0 :-)

This is one of the major features that EF4 will bring - the ability to set a relationship/association by using just the foreign key ID.

See:

Upvotes: 2

Related Questions