C# 如何附加不是来自数据库的实体框架对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/700192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I attach an Entity Framework object that isn't from the database?
提问by sontek
I have a complete separation of my Entity Framework objects and my POCO objects, I just translate them back and forth...
我完全分离了我的实体框架对象和我的 POCO 对象,我只是来回转换它们......
i.e:
IE:
// 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..
然后我有一个具有相同属性的 EF 对象“作者”..
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.
EntityKey 值为空的对象不能附加到对象上下文。
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?
编辑:看起来我必须使用 entity.AttachTo("Authors", dbAuthor); 在没有EntityKey的情况下附加,但是我有硬编码的魔法字符串,如果我根本改变我的实体集名称就会中断并且我不会有任何编译时检查......有没有办法我可以附加保持编译时检查?
I would hope I'd be able to do this, as hard coded strings killing off compile time validation would suck =)
我希望我能够做到这一点,因为硬编码的字符串杀死编译时验证会很糟糕 =)
采纳答案by Quintin Robinson
Have you tried using AttachToand specifying the entity set?..
您是否尝试过使用AttachTo并指定实体集?..
entities.AttachTo("Authors", dbAuthor);
where "Authors"
would be your actual entity set name.
"Authors"
您的实际实体集名称在哪里。
Edit:
Yes there is a better way (well there should be). The designer should have generated "Add" methodsto the ObjectContext for you which translate out to the call above.. So you should be able to do:
编辑:
是的,有更好的方法(应该有)。设计者应该已经为你生成了 ObjectContext 的“Add”方法,这些方法转化为上面的调用。所以你应该能够做到:
entities.AddToAuthors(dbAuthor);
which should literally be:
从字面上看应该是:
public void AddToAuthors(Authors authors)
{
base.AddObject("Authors", authors);
}
defined in the whateverobjectcontext.designer.cs file.
在whateverobjectcontext.designer.cs 文件中定义。
回答by Alex James
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.
现在才看到这个。如果您想将 Attach() 附加到 ObjectContext,即让实体框架相信数据库中已经存在实体,并且您想避免使用魔术字符串,即
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
并在此处介绍:提示 13 - 如何以简单的方式附加实体
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
并在此处进行了介绍:提示 16 - 今天如何模仿 .NET 4.0 的 ObjectSet
As you can see both are really easy to use and avoid strings altogether.
如您所见,两者都非常易于使用并且完全避免使用字符串。
Hope this helps
希望这可以帮助
Alex
亚历克斯
回答by Mat J
For me,
为了我,
Context.Authors.AddObject(new Author())
works perfectly. Wondering if I'm missing something? Or it is not the right way?
完美地工作。想知道我是否遗漏了什么?或者这不是正确的方法?
回答by sebagomez
Try this if you don't want to write the string with the EntitySet name
如果您不想使用 EntitySet 名称写入字符串,请尝试此操作
entities.AttachTo(entities.CreateObjectSet<T>().EntitySet.Name, entity);
回答by Roman O
Can you try following
你可以尝试跟随
entities.AddToAuthors(dbAuthor);
回答by Aran Dehkharghani
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);