In my MVC 4.0 and EF application I have added a new column in the database - updated Model from database in the edxm file. Made some changes in the controllers, build and published the site again. Also added the same column in the database on web server. After deploying the changes the insert code is not working. Its all working fine locally and I m not getting any error on the server and don't know how to go about troubleshooting this. Any ideas? thanks
Maybe it is silly but did you remember to change connectionstring ?
Related
I am trying to create a new web MVC project in Visual Studio 2015. I want to use my own database which already exists, and I want to add authentication models and controllers when creating the project.
The issue is:
When I create a new MVC project I can choose the type of authentication but I can't choose what database it uses. So the project is being created with its own local database while I want to use mine so that all necessary tables and DB objects are created in my own database.
I can't even find the LocalDB file created with the project to export the authentication data to my own database.
When I try to connect to the DB it asks me to browse for the mdf file and I have no clue where the file has been created. I did a search in Explorer for the mdf file but found nothing.
I got how to make it working! In case someone has the same problem these are the steps:
Visual Studio creates the defaultConnection - Yes.
But it doesn't create the tables (or probably even the database itself).
So I did the following:
Installed the project (ASP.NET Core Web Application with Framework template) using Individual User Accounts option as authentication.
Then went to appsettings.json and changed the connection string for my own database. (There is no needs to create asp auth tables, only have your own database ready)
I clicked debug and got the website displayed.
Clicked register and registered new user - I got the screen with button to do the migration.
I clicked the button and refreshed the page.
Went to my database and saw all tables have been created in there and the user inserted in the database.
I hope this helps anyone.
I am trying to create an ASP.NET MVC site loosely based on this tutorial.
http://www.windowsazure.com/en-us/develop/net/tutorials/web-site-with-sql-database/
I am using VS2013 preview which gave me the option to create an ASP.NET MVC 5 site. Also, using the following packages:
EntityFramework 6.0.0-rc1
Microsoft.AspNet.Identity.Core" 1.0.0-alpha1
Microsoft.AspNet.Identity.EntityFramework 1.0.0-alpha1
After configuring a website and associating it with a database in Azure, I proceeded to run the following commands in the package manager console:
enable-migrations -ContextTypeName IdentityDbContext
add-migration Initial
When I published to Azure with the database specified and the "Execute Code First Migrations" checkbox checked, the site deployed properly, but after inspecting my DB, the migrations didn't run. My migrations had been successful previously on a site with ASP.Net MVC 4 and EF 5.0.
I would guess the problem is that EF 6 is ignoring the connection string that is set during publishing, but I cannot understand why.
Any help would be appreciated. Thank you.
This guide looks comprehensive: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application
The key part being "Execute Code First Migrations (runs on application start)"
I hope that helps.
When I faced this, I needed to change the connection string to point to the Azure DB instead of the local SQLLite DB.
I find this site is very interesting and very helpful. I came up with this doubt while programming a project in asp.net mvc. I had to remove few columns from a view which I was using to pull data from the database back to my presentation layer. After removing those columns, I build my project and there were no errors. When I ran it, my project broke with error saying missing related columns as Entity was not changed.
But, my question is that is it necessary that every time I need to update my entity model?. What if I deploy my project in production and then had to remove one column from the view? It will be a hell of task for me If I had to update model, build and deploy it back again on production. Is there a simple solution for this?? thanks for your help
You have to update the entity model every time the database structure changes, what you need to do is to decouple the EF model from the MVC project, if you don't what to work with Ioc, a simpler approach whould be to place the EF model in a separate assembly, when the database changes, rebuild the assembly and deploy it on the server, this way you don't need to rebuild the entire MVC.
I've got an ASP.NET MVC 4 site with MSSQL database generated by code first approach. I want to use this database in another project. This project should crawl several tables from db, update them and send notification to users.
What is the best way to add existing database from the first project to the second project? I'm thinking about generating edmx by database, but this approach doesn't seem good enough.
You would be best moving the EF code and Entities into their own library, and then having your two different projects (Web and Windows Service) have a reference to that library.
Then, if and when your database structure or entities change, you only need to do this in one place.
If the second project is not going to modify the structure of the database, then I would use a data-first approach in your second project.
You can use codefirst mapping without problem on existing database. So simple reference assembly with datacontext in other project and it will work. (Don't forget set connection string in config file of other project)
Edited:
i'm trying to learn ASP MVC, but i'm having a problem, the Web Developer Express is failing to generate the database tables.
Please How i know where is the problem?
-
i'm just following the asp mvc website tutorial, and there the model class "make - create" the database + tables just pressing F5.... in my first tutorial all was perfect... but now i don't know what's happening
Thanks you guys :)
If you have already created a database you'll need to tell the app that you want to drop and recreate the tables when the database changes. Try adding this to Global.asax
System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<YourApp.Models.YourAppContext>());