I am coding a C# MVC 5 internet application and I have changed the context class. I wish to create a new database with the changed context class as I am getting the following error:
Invalid object name 'dbo.MapLocations'.
I have renamed the database name in the web.config, compiled and run the application, yet am still getting the above error.
How can I recreate the database to have the structure of my context class such that I do not get the above error? I thought that specifying a new database name will automatically create a new database using the entity framework.
I have no important data in the database.
Thanks in advance
EDIT
I have manually deleted the files in the App_Data folder for the project, enabled migrations, added a migration and then updated the database.
This is the error that I am getting in the package manager console:
Cannot attach the file 'C:\Users\**path**\App_Data\CanFindLocation.Context.DataContext.mdf' as database 'CanFindLocation.Context.DataContext'.
Try to use DropCreateDatabaseAlways class. Similar quetion here.
Be carefull, it always drops and recreates your database.
Related
I know a lot of answer for this question is available here but I have tried all out and not working. Maybe because my issue is different..
So i have an application already already in use with code-first approach. More than a year now. But there is the need to add some functionality which requires new fields to existing tables and also additional tables.
I am done adding this on local system. I want to do migration for it but bringing error
There is already an object named 'BookInvestments' in the database BookInvestment is an already existing table in the database by the way..
I have deleted the migration table on the Database and issue persist.
Please note that the namespace of the application remains same
I have also deleted the files created in the migration folder on my visual studio in trying to create new migrations and update database but issue persist.
I have records in the database and I cannot discard it as client has to make use of it going forward as well.
Please what do you suggest I do?
I'm working on an application with asp.net mvc that supports install, remove plugins.
When I want to install a new plugin I have an Install method that registers new routes and ...
For database, I use a code-first approach for creating database and every plugin has it's own context class.
My question is: when I want to install a new plugin, I need to create additional tables in my existing database, or create a new database if the database does not yet exist. And if those tables are already there, nothing should be created.
How do I achieve this?
Thanks in advance
Code First Migrations has two primary commands that you are going to become familiar with
Add-Migration will scaffold the next migration based on changes you
have made to your model since the last migration was created
Update-Database will apply any pending migrations to the database
When you develop a new application, your data model changes frequently, and each time the model changes, it gets out of sync with the database. You have configured the Entity Framework to automatically drop and re-create the database each time you change the data model. When you add, remove, or change entity classes or change your DbContext class, the next time you run the application it automatically deletes your existing database, creates a new one that matches the model, and seeds it with test data.
This method of keeping the database in sync with the data model works well until you deploy the application to production. When the application is running in production it is usually storing data that you want to keep, and you don't want to lose everything each time you make a change such as adding a new column. The Code First Migrations feature solves this problem by enabling Code First to update the database schema instead of dropping and re-creating the database.
I recommend to have look following link which makes you more clear about your problem.
https://msdn.microsoft.com/en-us/data/jj591621
I see that this question has been asked quite a bit, but none of the solutions have helped me since I'm using model first and not code first.
I have a C# project in VS 2015 using EF6. I am building a database using the model first approach and can successfully generate the SQL code from the model and run it in SSMS. I'm using SQL Server LocalDB.
The problem I have is that whenever I try to add a programmatically created entity to the collection (table) to which it belongs, I always get the error
An exception of type 'System.Data.Entity.Core.MetadataException' occurred in mscorlib.dll but was not handled in user code
Additional information: Schema specified is not valid.
Errors:
Market.ssdl(184,6) : error 0040: The Type nvarchar(max) is not qualified with a namespace or alias. Only primitive types can be used without qualification.
The entity I'm creating is has only one property; a string (or nvarchar(max) in the database). Again, I can create the object, but the moment I try to add it to its collection (or table) before saving any changes, I get the above error. I even tried not naming the Name property, but the error persists.
using (var context = new MarketContainer())
{
// Create data source
var datasource = new DataSource()
{
Name = dataSourceName
};
// Save data source
context.DataSources.Add(datasource);
}
Another SO answer proposed to right click on the .tt file and clicking "Run Custom Tool", but that didn't do anything.
I tried this once with MySQL and it worked fine! Now that I need to move to SQL Server it doesn't work... I've been stuck on this problem for over a week, so really any help would go a long way.
The issue has been resolved. I was referencing the project which contained the entity model from another project, and the App.configs were not matching. Once I copied the contents from the entity project's config file to the referencing project's, everything began working properly.
Hopefully this helps someone other than myself!
I have a created two separate C# web application using EF6 (one with Database First and other with Code First approach). everything is going on smoothly but after sometime it is required to merge both.
After merging , Database is same for Code First Approach.
Initial I was using 'MigrateDatabasetoLatestVersion' for database initializer.
So when I try to run my application , it throws error related to Table is already exist. I have also tried with Package manager console to update migration and it throws same error.
and so I was not able to run the new solution on same database.
For resolving this issue, I have changed database initializer to 'CreateDatabaseifNotExist'. and after that I am able to use this database with my new solutions.
But the Problem arise, when I tried to add a new column to existing table or Creating a new table with Code First approach. Since there is no migration so it skips to alter/add table and for that I have to manually run Alter/Add table Script with each changes.
Is there any valid solution for this?
either MigrateDatabasetoLatestVersion doesn't throw exception or it is not required to run db script each time.
Thanks.
The problem that when i change the new database then application not detected the new database and retreive error
The model backing the 'DBContext' context has changed since the
database was created. Consider using Code First Migrations to update
the database (http://go.microsoft.com/fwlink/?LinkId=238269).
but i don't using Migrations so i don't update-database by Package Manager Console
How can i fix this problem?
Actually it does detect changes in your database. The database differs from the model. The error message.
This error is thrown when the hash stored in the migrations history table does not match a calculated hash. (Have a look at __MigrationHistory table, might be under system tables)
If you delete this table the check is essentially disabled. You can achieve something similar by adding Database.SetInitializer<YourContextType>(null) to the startup of your application. The latter disables database creation from within that application, so if you want to create the database by code, you would need to do this from a separate application (for example an console app). I prefer to go this way for web applications.
Secondly: if you change your database manually (change columns, add tables, etc.) you need to adjust your model. So for each DDL statement, change your code.
If you are not using code first, you could update your existing model in the designer.
I ran into this problem when I first started with code first and mvc. the answer below is absolutely correct but you should go to the ASP.net website and do some tutorials on code first migrations. you need a better understanding how update database and initialize and migrations work.