MVC 3 CodeFirst 与我的所有模型共享 SQL Express 数据库
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实例化多个 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.
您必须将数据库直接部署到 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 useAttachDBFile
.我在所有模型中设置单独的数据库上下文。我知道,这是一个多么菜鸟的错误。不管怎样,感谢您的快速回复。 这个帮助我弄清楚了它,它是这方面真正令人惊奇的资源
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