Changing a model's datasource in MVC2 for release?
If you're anxious to get to the point of this, just skip to the last paragraph.
I'm totally new to MVC2 and I've been tasked with creating a database management application with it. After spending a few weeks getting the hang of it, I've finally begun writing my production code. In order to test my code out, I need to be able to access the copy of the database on a dev server. When it's time to put a release version up, it obviously needs to work with the live database.
Initially, I created two side-by-side models (using the entity framework) of the maintenance database and just changed the namespace of the other one to something like DevModels
and opposed to just Models
in order to avoid name clashes (they're the exact same model, just different sources). This caused more problems than it solved. Anyway, while pouring through the book Pro ASP.NET MVC 2 Framework, I got the bright idea to just change the datasource in the Web.Config
file. This way, when I publish the app, it will automatically have the correct datasource. Long story short, this didn't exactly work and caused more problems than it solved.
Anyway, I've done some looking around but I just can't find anything. All the material I find is rather unspecific and leaves me more confused. I'm sure there's a simple way to do this as it HAS to be a common thing.
So, in summary, how do I write an MVC2 application once and be able to use the model with different datasources so I don't have to heavily modify code to make a production version of my application?
UPDATE: Here's the connection string before and after I change it. Before=dev server
`metadata=res:///Models.MaintModel.csdl|res:///Models.MaintModel.ssdl|res://*/Models.MaintModel.msl;provider=System.Data.SqlClient;provider connection string='Data Source=EXP_POWERSUITE2;Initial Catalog="Maint DatabaseSQL";Integrated Security=True;MultipleActiveResultSets=True'"
`metadata=res:///Models.MaintModel.csdl|res://开发者_如何学Python/Models.MaintModel.ssdl|res://*/Models.MaintModel.msl;provider=System.Data.SqlClient;provider connection string='Data Source=EXP_POWERSUITE;Initial Catalog="Maint DatabaseSQL";Integrated Security=True;MultipleActiveResultSets=True'"
Also, in order to make sure I had the right connection string, I started a new app and made a new edm with the same names and everything but geared towards to live database. I was looking for any obvious differences but it was the exact same thing as what I have above.
Also, I can retrieve and display rows from the database in my application. I just can't add them.
I'm using MS SQL Server Manager 2008 R2.
You can get the desired results by changing the connection string in the web.config to point to the new datasource. This will only work if the new database you are pointing to has all the tables, columns, references, etc as your development database. If it does not match, then your model will not match the new database and you will get errors.
If you decided to make a change to your development database, then you must make sure that change is in the new database you are pointing to or otherwise the model will not match.
Just changing the connectionstring in the web.config when publishing is what I use and what I'd recommend, what kind of problmes did it cause?
If the database structure is the same, it should work.
精彩评论