如何使用sql到实体处理联结表的数量列

时间:2021-12-06 07:25:21

I have 3 tables. Blog and Tag have a many to many relationship. BlogTag is a junction table with a quantity column.

我有3张桌子。博客和标签有很多关系。 BlogTag是一个包含数量列的联结表。

**Blog**
BlogID
Title

**Tag**
TagID
Name

**BlogTag**
BlogID
TagID
Quantity

I'm not sure how I handle the quantity column. I'd like it to store how many Blogs have a certain Tag Name

我不确定如何处理数量列。我希望它存储有多少博客有一个特定的标签名称

How do I deal with the quantity column when adding a new blog that has tags?

添加包含标记的新博客时,如何处理数量列?

Thanks!

1 个解决方案

#1


1  

Well, you'd need to calculate the quantity:

那么,你需要计算数量:

var q = (from b in Context.Blogs
         where b.BlogTags.Any(t => t.Tag.TagId == someId)
         select b).Count();

So you:

1. Add the blog
2. SaveChanges
3. For each tag on the new blog:
    1. Calculate the quantity, as above.
    2. Update the BlogTag.Quantity.
4. SaveChanges

#1


1  

Well, you'd need to calculate the quantity:

那么,你需要计算数量:

var q = (from b in Context.Blogs
         where b.BlogTags.Any(t => t.Tag.TagId == someId)
         select b).Count();

So you:

1. Add the blog
2. SaveChanges
3. For each tag on the new blog:
    1. Calculate the quantity, as above.
    2. Update the BlogTag.Quantity.
4. SaveChanges