0

I want to run several ASP.NET MVC projects using Entity Framework on a single SQL Server database. Is this possible? For example, using different schemas for different projects?

Or will the different projects overwrite the database?

I made a sample solution with two projects, connecting to the same database, that (predictably) showed the error message "the model has changed". The second EF project will of course assume that I made changes in the database and suggest a migration.

I can't figure out a good search phrase - I keep getting search hits unrelated to what I'm trying to do. So I'm pretty much out of ideas.

Edit: error in title, meant to write single SQL Server database

David
  • 943
  • 1
  • 10
  • 26
  • 3
    ***OF COURSE!*** SQL Server wouldn't be much of a **relational database system** if it didn't support multiple databases on a single server ..... each separate project typically has its own, **separate** database to avoid tripping of another project's data... – marc_s Apr 25 '16 at 13:47
  • You didn't explain properly _what exactly_ you are trying to do. The error you're getting relates to when you want to have entirely different Code First DbContexts on the same database. However, as marc_s indicates, you can have multiple databases in one server. If you want unrelated tables in one database, you'll have to ask yourself why and mention that (and other details) in your question. – CodeCaster Apr 25 '16 at 13:51
  • @marc_s I made a mistake in the title, forgot the word *database*... – David Apr 25 '16 at 13:58
  • @CodeCaster I need several applications on the same server, and I am unable to create more databases. So I think I need unrelated tables in the same database. – David Apr 25 '16 at 14:00
  • Sure why not it should work – jamiedanq Apr 25 '16 at 14:00
  • Well then search for "ef code first multiple dbcontext one database", first hit: http://stackoverflow.com/questions/11197754/entity-framework-one-database-multiple-dbcontexts-is-this-a-bad-idea – CodeCaster Apr 25 '16 at 14:01
  • You will need to take care not to use a database initializer that drops the database. https://blog.oneunicorn.com/2013/05/28/database-initializer-and-migrations-seed-methods/ – Steve Greene Apr 25 '16 at 14:09
  • @CodeCaster Each application is made by a different team and is not in the same solution. So the context files are completely separate. I don't see how multiple dbcontexts would help in this case. – David Apr 25 '16 at 14:11
  • 1
    Then just create a database per application, or don't use migrations. – CodeCaster Apr 25 '16 at 14:12
  • But I can't create new databases (because of my hosting provider) without paying more, so I want to be certain – David Apr 25 '16 at 14:15
  • 1
    If you jump through some hoops you can do it. Make sure the database objects don't step on each other (schemas, naming conventions, etc). I would use separate databases on the DEV side then script out changes when you want to update the hosted database (update-database -Script). You will need to remove the __MigrationHistory references since those would be meaningless with multiple projects hitting it. Use a null (or custom) initializer for hosted deployment. You are essentially trusting that your code model matches the database and that you know what migrations have been deployed. – Steve Greene Apr 25 '16 at 19:04

0 Answers0