MVC 3 CodeFirst Share sql express database with all of my models
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 migh开发者_如何学Ct 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.
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.
You must deploy your database directly to SQL Server instead of using local file in AppData
=> your connection string mustn't use AttachDBFile
.
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
精彩评论