I have 2 tables:
我有2张桌子:
tblAuthors
- id
- username
tblPosts
- id
- authorid
So if I create a new Post
所以,如果我创建一个新帖子
var post = new Post { Id=Guid.NewId(), Author=new Author { UserName="username", Id="id in database } };
The author of the post is already in the database, but the post itself is not...
该帖子的作者已经在数据库中,但帖子本身不是......
So I want to do:
所以我想做:
entities.AddToPosts(obj);
but it tries to add the Author and the Post... is there a way to have it Save the author and add the post? Or just ignore the Author completely?
但它试图添加作者和帖子...有没有办法让它保存作者并添加帖子?或者完全忽略作者?
I have both the Post and the Author as my own POCO business objects, that were pulled from the database and then converted to my business layer. I don't want to have to re-pull them because I could have modified either in my business layer. So I just want to attach the objects.
我将Post和Author都作为我自己的POCO业务对象,从数据库中提取,然后转换为我的业务层。我不想重新拉它们,因为我可以在我的业务层中进行修改。所以我只想附加对象。
I don't want to keep the data objects around, I pull them out and convert them to business objects and then don't touch them again until I want to push back to the database and I convert the business objects to data objects.
我不想保留数据对象,我将它们拉出来并将它们转换为业务对象,然后再次触摸它们,直到我想要回到数据库并将业务对象转换为数据对象。
4 个解决方案
#1
Shouldn't you first retrieve the Author record from the database to be able to set it in the Post object?
你不应该首先从数据库中检索作者记录,以便能够在Post对象中设置它吗?
So you would you get:
所以你会得到:
var author = Author.Select(a => a.Id).FirstOrDefault();
var post = new Post { Id=Guid.NewId(), Author=author };
entities.AddToPosts(obj);
Maybe you could use the Attach and AttachTo ObjectContext to attach the object, but is not mentioned in the MSDN library whether a query to the database is executed. With some tracing you could check whether a database query occurs.
也许你可以使用Attach和AttachTo ObjectContext来附加对象,但是在MSDN库中没有提到是否执行了对数据库的查询。通过一些跟踪,您可以检查是否发生数据库查询。
#2
It is possible to mock a "well-known" entity, without hitting the database. Create a new entity object, set the properties of the entity that are used in the entity key, and call ObjectContext.AttachTo. This will add the entity to the context in the Unchanged state, and can now be referenced by other entities.
可以模拟“众所周知的”实体,而无需访问数据库。创建一个新的实体对象,设置实体键中使用的实体的属性,并调用ObjectContext.AttachTo。这会将实体添加到Unchanged状态的上下文中,现在可以被其他实体引用。
// AttachTo
var item = new Item() { ItemId = wellKnownId };
context.AttachTo( "Items", item );
Assert.AreEqual( EntityState.Unchanged, item.EntityState );
#3
I have a hunch that functionality you are seeking for was announced for next version of EF. There was a whole topic related to entity associacions @ mix.
我有一种预感,你正在寻找的功能是为下一版EF宣布的。整个主题与实体关联@mix有关。
Or you can try to map AuthorId Column from db to entity as well. (it does not map by default) You can't map foreign keys, so you can't associate an object without getting one from db.
或者您也可以尝试将AuthorId Column从db映射到实体。 (默认情况下不映射)您无法映射外键,因此如果没有从db获取对象,则无法关联该对象。
EDIT: and this is why I'm still using linq2sql which provides this functionality.
编辑:这就是为什么我仍然使用提供此功能的linq2sql。
#4
This functionality isn't completely supported in EF yet, I had to the following:
EF尚未完全支持此功能,我必须执行以下操作:
var post = new Post { Id=Guid.NewId(), AuthorReference.EntityKey = new EntityKey("Authors", "Id", authorIdValue) };
entities.AddToPost(post);
#1
Shouldn't you first retrieve the Author record from the database to be able to set it in the Post object?
你不应该首先从数据库中检索作者记录,以便能够在Post对象中设置它吗?
So you would you get:
所以你会得到:
var author = Author.Select(a => a.Id).FirstOrDefault();
var post = new Post { Id=Guid.NewId(), Author=author };
entities.AddToPosts(obj);
Maybe you could use the Attach and AttachTo ObjectContext to attach the object, but is not mentioned in the MSDN library whether a query to the database is executed. With some tracing you could check whether a database query occurs.
也许你可以使用Attach和AttachTo ObjectContext来附加对象,但是在MSDN库中没有提到是否执行了对数据库的查询。通过一些跟踪,您可以检查是否发生数据库查询。
#2
It is possible to mock a "well-known" entity, without hitting the database. Create a new entity object, set the properties of the entity that are used in the entity key, and call ObjectContext.AttachTo. This will add the entity to the context in the Unchanged state, and can now be referenced by other entities.
可以模拟“众所周知的”实体,而无需访问数据库。创建一个新的实体对象,设置实体键中使用的实体的属性,并调用ObjectContext.AttachTo。这会将实体添加到Unchanged状态的上下文中,现在可以被其他实体引用。
// AttachTo
var item = new Item() { ItemId = wellKnownId };
context.AttachTo( "Items", item );
Assert.AreEqual( EntityState.Unchanged, item.EntityState );
#3
I have a hunch that functionality you are seeking for was announced for next version of EF. There was a whole topic related to entity associacions @ mix.
我有一种预感,你正在寻找的功能是为下一版EF宣布的。整个主题与实体关联@mix有关。
Or you can try to map AuthorId Column from db to entity as well. (it does not map by default) You can't map foreign keys, so you can't associate an object without getting one from db.
或者您也可以尝试将AuthorId Column从db映射到实体。 (默认情况下不映射)您无法映射外键,因此如果没有从db获取对象,则无法关联该对象。
EDIT: and this is why I'm still using linq2sql which provides this functionality.
编辑:这就是为什么我仍然使用提供此功能的linq2sql。
#4
This functionality isn't completely supported in EF yet, I had to the following:
EF尚未完全支持此功能,我必须执行以下操作:
var post = new Post { Id=Guid.NewId(), AuthorReference.EntityKey = new EntityKey("Authors", "Id", authorIdValue) };
entities.AddToPost(post);