linq to sql datacontext,插入对象并检索id并将其附加到另一个表?
奇怪的问题标题...
我认为我想要完成的事情非常简单。
我在数据库中有 3 个表,
BlogPost - BlogPostTagsConnection - Tags
博客文章包含文本、标题、slug、作者、id 等。包含 BlogPost id 和 Tags id,最后 Tags 包含 tagid 和 tag。
因此,我试图弄清楚如何插入新记录(并最终更新一条记录):
public void Create(BlogPost post)
{
DB db = new DB();
var matchingTags = from data in db.Tags
where (from tagsInPost in post.Tags select tagsInPost.TagName).Contains(data.Tag)
select new Tag() {TagId = data.TagID, TagName = data.Tag};
var post2 = new DataBlogPost
{
Title = post.Title,
Text = post.Text,
Slug = post.Slug,
ShortDescription = post.ShortDescription,
Published = post.Published,
Author = post.Author
};
db.DataBlogPosts.InsertOnSubmit(post2);
... <- insert the tags and retrieve the id from the tags and the blogpost
... <- insert the ids from the tags and blogposts to the blogpoststagsconnection table
db.SubmitChanges();
}
首先,我从数据库中获取与我在博客文章中编写的标签相匹配的所有标签。标签保存在带有 Tags { TagID, Tag } 的列表中。
其次,我将新帖子插入数据库(post2)。
之后我需要插入所有标签(我用我的博客文章创建的),所以如果我在标签文本框中写下这个“tag1 tag2 tag3 tag4”并且我的数据库已经包含tag1和tag2,那么它应该创建2条新记录,一个用于 tag3,一个用于 tag4。
然后,我需要以某种方式从生成的博客文章中获取 id,并从 tag3 和 tag4 中获取 id,以便能够将它们插入到 BlogPostTagsConnection 中。
但如何取回身份证呢?或者有更简单的方法来处理这些插入吗?
Wierd question title...
What i am trying to accomplish is pretty simple i think.
I got 3 tables in the database,
BlogPost - BlogPostTagsConnection - Tags
The blogpost contains text, title, slug, author, id etc. The BlogPostTagsConnection contains the BlogPost id and the Tags id and finally the Tags contains tagid and tag.
So i tried to figure out how to insert a new record (and eventually to update one):
public void Create(BlogPost post)
{
DB db = new DB();
var matchingTags = from data in db.Tags
where (from tagsInPost in post.Tags select tagsInPost.TagName).Contains(data.Tag)
select new Tag() {TagId = data.TagID, TagName = data.Tag};
var post2 = new DataBlogPost
{
Title = post.Title,
Text = post.Text,
Slug = post.Slug,
ShortDescription = post.ShortDescription,
Published = post.Published,
Author = post.Author
};
db.DataBlogPosts.InsertOnSubmit(post2);
... <- insert the tags and retrieve the id from the tags and the blogpost
... <- insert the ids from the tags and blogposts to the blogpoststagsconnection table
db.SubmitChanges();
}
First i am getting all the tags from the database that matches the tags that i wrote in my blogpost. The tags are saved in a List with Tags { TagID, Tag }.
Secondly i insert my new post into the database (post2).
After this i need to insert all the tags (that i have created with my blogpost), so if i wrote this in the tags textbox "tag1 tag2 tag3 tag4" and my database already contains tag1 and tag2, it should then create 2 new records, one for tag3 and one for tag4.
I then need to get the id from the generated blogpost somehow and also the id from the tag3 and tag4 to be able to insert those into the BlogPostTagsConnection.
But how do i get the ID back? or is there a simpler way to handle these inserts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,如果您的 FK 关系已正确设置并且表已正确导入到 DBML 表面,您的
DataBlogPost
将具有一个属性BlogPostTagsConnections
(或类似属性),您可以使用新属性填充该属性(或从同一个 DataContext 中提取的现有)BlogPostTagsConnection
实例。同样,
BlogPostTagsConnection
将有一个可以填充的属性Tags
。完成后,插入您的
DataBlogPost
,所有关系数据也将被保存。无需担心 ID,只需将现有或新实例添加到这些“关系”属性中即可。Yes, if your FK relationships are properly set up and tables properly imported on your DBML surface, your
DataBlogPost
will have a propertyBlogPostTagsConnections
(or similar) which you can populate with new (or existing, pulled from the same DataContext)BlogPostTagsConnection
instances.Similarly
BlogPostTagsConnection
will have a propertyTags
which you can populate.When done, insert your
DataBlogPost
and all the relational data will be saved too. No need to worry about IDs, just add existing or new instances to these "relational" properties.coster 是对的,
还有一些关于如何检索插入的实体 id 的附加信息:
插入实体后,只需读取该实体的 ID 属性,即可实现。
spender is right,
and some additional information on how to retrieve the inserted entity id:
After inserting the entity, simply read the ID property for that entity, it'll be fulfilled.