MVC 3 CodeFirst 与我的所有模型共享 SQL Express 数据库

发布于 2024-11-29 08:07:56 字数 152 浏览 4 评论 0原文

我正在使用 EF 和代码优先创建一个使用 mvc3 的数据驱动网站。我试图让我的所有模型共享一个数据库,以便不同的角色可以访问公用表。我不确定是否只使用一个数据库上下文,因为这似乎可能是错误的方式。我不希望我的模型创建自己的单独数据库。我希望他们分享,但我似乎无法实现这一点。感谢任何帮助。

I am creating a data driven website with mvc3 using EF and code first. I am trying to have all of my models share one database so that seperate roles can have access to common tables. I am not sure weather to just use one db context because that seems like it might be the wrong way. I do not want my models to create their own seperate databases. I want them to share but I cant seem to achieve this. Thank to any help.

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

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

发布评论

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

评论(3

此岸叶落 2024-12-06 08:07:56

实例化多个 DbContext 不会导致您拥有多个数据库,除非您将不同的连接字符串传递到每个数据库中。如果您有一个连接字符串(通常保存在 web.config 文件中),那么您可以拥有多个 DbContext,但它们都将共享同一个数据库。

Instantiating multiple DbContexts will not cause you to have multiple databases unless you pass different connection strings into each one. If you have a single connection string, which will typically be kept in your web.config file, you can have many DbContexts, but they will all share the same database.

淡水深流 2024-12-06 08:07:56

您必须将数据库直接部署到 SQL Server,而不是在 AppData => 中使用本地文件。您的连接字符串不得使用 AttachDBFile

You must deploy your database directly to SQL Server instead of using local file in AppData => your connection string mustn't use AttachDBFile.

苍暮颜 2024-12-06 08:07:56

我在所有模型中设置单独的数据库上下文。我知道,这是一个多么菜鸟的错误。不管怎样,感谢您的快速回复。 这个帮助我弄清楚了它,它是这方面真正令人惊奇的资源

I was setting seperate DB contexts in all of my Models. I know, what a noob mistake. Thanks for the quick responses anyhow. THIS helped me figure it out and its really and amazaing resource for this stuff

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