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

6

23

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 =)

Riata answered 31/3, 2009 at 6:4 Comment(6)
Did you look at the edit to my answer, it will allow you to do it without magic strings or AttachTo as the methods are designer generated and maintained by the edmx model.Piano
Yes, I'm not adding it, i'm updating it, so I need to attach to it, and save changesRiata
I still believe your model has an issue with the Id property on the Authors object not being set as the EntityKey, you should double check the XML generated and make sure it is nested in a key element and make sure the Id property has the attribute EdmScalarPropertyAttribute(EntityKeyProperty=true)Piano
It has: <EntityType Name="tblAuthors"> <Key> <PropertyRef Name="Id" /> </Key> but it doesn't have anything about EdmScalarPropertyAttributeRiata
The EdmScalarPropertyAttribute would be applied to the Id property inside of the designer.cs file for the model. It should definitely have the attribute, but I'm thinking the EntityKeyProperty parameter may not be specified.Piano
It has: [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]Riata
P
13

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.

Piano answered 31/3, 2009 at 6:22 Comment(2)
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?Riata
@Ashkan Your edit makes the answer incorrect, the automatically generated AddToAuthors doesn't use EntitySet.Name, and your edit claims it does, so I've reverted it. The principle behind your edit looks good to me, and that's what's used in another answer, which I've upvoted, and I encourage you to do the same.Overstuff
F
15

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

Frierson answered 8/5, 2009 at 6:51 Comment(0)
P
13

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.

Piano answered 31/3, 2009 at 6:22 Comment(2)
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?Riata
@Ashkan Your edit makes the answer incorrect, the automatically generated AddToAuthors doesn't use EntitySet.Name, and your edit claims it does, so I've reverted it. The principle behind your edit looks good to me, and that's what's used in another answer, which I've upvoted, and I encourage you to do the same.Overstuff
C
5

Try this if you don't want to write the string with the EntitySet name

entities.AttachTo(entities.CreateObjectSet<T>().EntitySet.Name, entity);
Cismontane answered 16/11, 2010 at 3:32 Comment(1)
'entities.Authors.Attach(dbAuthor);' is nicer.Anglice
A
4

an alternative to this is as following in particular if you don't know which entity you are going to operate on:

string className = entityObject.GetType().Name;
var container = _DbContext.MetadataWorkspace.GetEntityContainer(_DbContext.DefaultContainerName, DataSpace.CSpace);
string setName = (from meta in container.BaseEntitySets
                  where meta.ElementType.Name == className
                  select meta.Name).First();
_DbContext.AttachTo(setName, entityObject);
Attendance answered 15/11, 2012 at 3:10 Comment(1)
Thank you for this, no idea how you figured this out but I happened to run across it when looking for a way to do exactly this. I'm using the Code First Stored Procedures project from codeproject.com/articles/179481/code-first-stored-procedures and was not happy that the items weren't added to DbContext tracking. Using this code, I was able to add them. It's not perfect because they are added without the proxy code to lazy load properties, but it works well enough for me.Bethina
K
0

Can you try following

entities.AddToAuthors(dbAuthor);
Kingpin answered 21/8, 2012 at 19:57 Comment(2)
This is really a comment, not an answer to the question. You can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.Felipa
@BryanCrosby I disagree. I believe this is an answer, not a comment.Recrimination
S
-2

For me,

Context.Authors.AddObject(new Author())

works perfectly. Wondering if I'm missing something? Or it is not the right way?

Sampson answered 13/10, 2010 at 14:36 Comment(1)
This is used for new objects, not for existing ones.Anglice

© 2022 - 2024 — McMap. All rights reserved.