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

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.

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.

1 This does seem to work, but then I have "magic strings" every where, and if I rename the entity set all my code will break. Is there a better way to do this? – sontek Mar 31 '09 at 6:35.

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.

Try this if you don't want to write the string with the EntitySet name entities. AttachTo(entities.CreateObjectSet().EntitySet. Name, entity).

Entities.Authors. Attach(dbAuthor);' is nicer. – ReinierDG Feb 10 at 15:00 I used my code in a generic method where I can gat any type of entity... – sebastian Feb 23 at 22:07.

For me, Context.Authors. AddObject(new Author()) works perfectly. Wondering if I'm missing something?

Or it is not the right way?

1 This is used for new objects, not for existing ones. – ReinierDG Feb 10 at 14:58.

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.

You can try two possibilities based on extension methods, both of which definitely make life more bearable. And is covered here: Tip 16 - How to mimic . As you can see both are really easy to use and avoid strings altogether.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions