在实体框架中实现导航属性

发布于 2024-09-25 22:55:23 字数 485 浏览 0 评论 0原文

嘿伙计们, 我一直在学习 MVC 2,除了模型部分之外,我几乎了解了所有内容,我理解模型是什么,但实际实现它让我感到困惑。

这是我的情况,我的数据库有 3 个表;

想法 - 想法表 标签 - 标签表 IdeaTag - 通过 FK 连接上述 2 个表的链接表

因此,当在 VS2010 中使用实体框架 (.edmx) 设计器时,我在设计器中创建了 2 个类,它们显然映射到我的 DB 表和 Idea for Tags & 中的导航属性。标签的想法。

所以这一切都很好,直到我真正尝试向一个想法添加标签为止,处理导航属性的最佳实践是什么?我想添加一个文本框,然后将其映射到 Idea 类中的 Tags 属性,但我不确定如何进行此操作。

大多数讨论 EF 或 Linq to SQL 的 MVC 教程都非常基础。

如果有任何关于讨论处理导航属性以及如何最好地处理它们的教程或视频教程的建议,我很乐意接受。或者,如果有更好的方法来实现这一点,我也很乐意研究。

Hey folks,
I've been learning MVC 2 and I have pretty much everything understood except for the model part of things, I understand what the model is but actually implementing it has me confused.

Here's my situation, I have my DB which has 3 tables;

Ideas - table of ideas
Tags - table of tags
IdeaTag - link table connecting the above 2 tables via FKs

So when using the Entity Framework (.edmx) designer in VS2010 I get 2 classes created in the Designer, which obviously map to my DB tables and Navigation Properties in Idea for Tags & Idea for Tag.

So this is all fine until I actually try to add tags to an idea, what's the best practise for dealing with Navigation Properties? I wanted to add a Textbox which will then map to the Tags property in the Idea class but I'm unsure how I'd go about this.

Most of the MVC tutorials which discuss EF or Linq to SQL are quite basic.

If there are any suggestions for tutorials or video tutorials which discuss dealing Navigation Properties and how best to deal with them I'd gladly take it on board. Alternatively if there is a better way to implement this I'd happily look into that too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫腻 2024-10-02 22:55:23

鉴于这基本上是多对多关系,请查看此问题的已接受答案。

它涵盖了您正在寻找的场景。在你的情况下,“ID”可能是实际的标签,即“实体框架”或其他东西。

使用该示例中的方法,您可以拥有一个文本框,其中包含逗号(或其他内容)分隔的标签名称列表。拆分文本框的内容会为您提供一组标签,完成后这些标签应该位于 Idea.Tags 集合中。

我猜你唯一遇到的麻烦是,你想自动创建“新”标签。因此,也许您可​​能需要在附加标签之前检查标签是否存在(将其添加到 Idea.Tags 集合中),如果它不存在,则只需添加它即可,而不是附加它,以便插入新标签。

Seeing as this is basically a Many to Many relationship, check out the accepted answer on this question.

It covers the scenario you are looking for. In your case 'ID' is probably the actual tag, i.e. "Entity Framework" or something.

Using the approach in that sample you can have a textbox, with a comma (or something else) delimited list of tag names. Splitting the contents of the textbox gives you an array of tags that should be in the Idea.Tags collection after you're done.

The only complication you have is I guess, you want to automatically create 'new' tags. So perhaps you'll probably need to check whether the Tag exists before attaching it (to add it to the Idea.Tags collection), if it doesn't exist instead of attaching it you would simply add it, so the new tag gets inserted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文