Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I attach an Entity Framework object that isn't from the database?

Tags:

I have a complete separation of my Entity Framework objects and my POCO objects, I just translate them back and forth...

i.e:

// poco public class Author {    public Guid Id { get; set; }    public string UserName { get; set; } } 

and then I have an EF object "Authors" with the same properties..

So I have my business object

var author = new Author { UserName="foo", Id="Guid thats in the db" }; 

and I want to save this object so I do the following:

var dbAuthor = new dbAuthor { Id=author.Id, UserName=author.UserName }; entities.Attach(dbAuthor); entities.SaveChanges(); 

but this gives me the following error:

An object with a null EntityKey value cannot be attached to an object context.

EDIT: It looks like I have to use entities.AttachTo("Authors", dbAuthor); to attach without an EntityKey, but then I have hard coded magic strings, which will break if I change my entity set names at all and I wont have any compile time checking... Is there a way I can attach that keeps compile time checking?

I would hope I'd be able to do this, as hard coded strings killing off compile time validation would suck =)

like image 734
sontek Avatar asked Mar 31 '09 06:03

sontek


People also ask

What is the primary difference between the add and attach methods on a DbContext?

Add is to indicate to the Entity Framework that we are going to want to insert a new record in the database. In contrast, Entry and Attach are used in scenarios where the record already exists in the database, and we simply want to make some kind of modification on it. var attach = context.

What is DbSet attach?

Attach is used to repopulate a context with an entity that is known to already exist in the database. SaveChanges will therefore not attempt to insert an attached entity into the database because it is assumed to already be there.


2 Answers

Just seeing this now. If you want to Attach() to the ObjectContext, i.e. convince the entity framework that an entity exists in the database already, and you want to avoid using magic strings i.e.

ctx.AttachTo("EntitySet", entity); 

You can try two possibilities based on extension methods, both of which definitely make life more bearable.

The first option allows you to write:

ctx.AttachToDefault(entity); 

and is covered in here: Tip 13 - How to attach an entity the easy way

The second option allows you to write:

ctx.EntitySet.Attach(entity); 

and is covered here: Tip 16 - How to mimic .NET 4.0's ObjectSet today

As you can see both are really easy to use and avoid strings altogether.

Hope this helps

Alex

like image 190
Alex James Avatar answered Oct 12 '22 01:10

Alex James


Have you tried using AttachTo and specifying the entity set?..

entities.AttachTo("Authors", dbAuthor); 

where "Authors" would be your actual entity set name.

Edit:
Yes there is a better way (well there should be). The designer should have generated "Add" methods to the ObjectContext for you which translate out to the call above.. So you should be able to do:

entities.AddToAuthors(dbAuthor); 

which should literally be:

public void AddToAuthors(Authors authors) {     base.AddObject("Authors", authors); } 

defined in the whateverobjectcontext.designer.cs file.

like image 38
Quintin Robinson Avatar answered Oct 12 '22 01:10

Quintin Robinson