How to reset database migrations using EF code first? - c#

I have a class library project with my DbContext and migration enabled with following config file:
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<connectionStrings>
<add name="DataContext" connectionString="Data Source=Data.sdf" providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlServerCe.4.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
Some time before I played with migrations for this project and Get-Migrations command always returns following to me:
PM> Get-Migrations -StartupProjectName "Data"
Retrieving migrations that have been applied to the target database.
201207012104355_Initial
201207012031234_Initial
201207012024250_Initial
The problem is that command always returns these items even if I delete Data.sdf or delete all project and make new one. The only way I can create new database is changing database file name in connection string from Data.sdf to Data1.sdf for example.
So how can i reset migration history without changing database name?

Don't know if this is the official method. But here's how I did it.
Deleted migration file
Deleted matching row from __MigrationHistory
DELETE FROM __MigrationHistory WHERE MigrationId='201210271944168_AddLogTable'

Related

Migrating database from MySQL to SQL Server, Entity Framework still tries to use MySQL

Until recently, I used a MySQL database with Entity Framework. I have migrated this database to SQL Server.
For some reason however, Entity Framework still tries to use the MySQL provider for EF:
MetadataException: Schema specified is not valid.
Errors: (0,0) : error 0175: The ADO.NET provider with invariant name 'MySql.Data.MySqlClient' is either not registered in the machine or application config file, or could not be loaded. See the inner exception for details.
What I've tried so far:
I have removed every dependency and reference to MySQL and it's provider, including any NuGet packages
I've completely removed and re-installed Entity Framework
Obviously, I changed my connection string
I've even cleared all my app.config files (it's a WPF application) and reinstalled EF to start with a clean configuration
If I search my entire solution for MySQL, no results can be found. Still, this error occurs.
What could still cause this error?
My app.config (in case it matters):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false" />
</configSections>
<connectionStrings>
<add name="sqlservercon"
connectionString="Data Source=<url>;Initial Catalog=<dbname>;User Id=<user>;Password=<pw>;"
providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory
type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient"
type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
Context:
public class CustomContext : DbContext
{
public DbSet<Member> Members { get; set; }
...
public CustomContext()
: base("name=sqlservercon")
{
}
}

Entity Framework 6 (code first) Add-Migration puts wrong provider to the edmx schema

I have a project which is originally used System.Data.SqlServerCe.4.0 (MSSQL Compact Edition 4.0) data provider. Then I've made a branch, changed config file and references for System.Data.SqlClient (MSSQL provider), uninstalled EntityFramework for MSSQLCE nuget package. Everything in the branch project works fine except two things: Add-Migration and Database seed script fail with the next error:
Schema specified is not valid. Errors: (0,0) : error 0175: The ADO.NET provider with invariant name 'System.Data.SqlServerCe.4.0' is either not registered in the machine or application config file, or could not be loaded
I've tried to find the source of the issue and finally found that EDMX schema, which is stored in the __MigrationHistory of the MSSQL project database, contains the next line:
<Schema Namespace="CodeFirstDatabaseSchema" Provider="System.Data.SqlServerCe.4.0" ProviderManifestToken="4.0" Alias="Self" xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
But I don't understand, why it is generated with the Provider="System.Data.SqlServerCe.4.0" option ? I've checked that my branch project doesn't have any file, which contains SqlServerCe substring (did file search in OS). I've tried to reinstall EntityFramework NuGet package, doesn't help as well.
App.Config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="AppDBContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=AnalyticDB;Persist Security Info=True;Trusted_Connection=Yes;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
EF version: 6.1.2
Thanks!

Upgraded EF5 to EF6 - getting No Entity Framework provider found

I have updated my project from EF5 to EF6, using NuGet, but am getting an error now as soon as my application tried to query the database.
error 0152: No Entity Framework provider found for the ADO.NET
provider with invariant name 'System.Data.SqlClient'. Make sure the
provider is registered in the 'entityFramework' section of the
application config file. See
http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
I think the issue is with my data project's App.Config, which resembles this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="AccuFinanceEntities" connectionString="metadata=res://*/AccuFinanceEntities.csdl|res://*/AccuFinanceEntities.ssdl|res://*/AccuFinanceEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=laptop;initial catalog=mydb;Integrated Security=True;MultipleActiveResultSets=True;App=AccuFinance"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
My web.config only has a connection string applied:
<connectionStrings>
<!--SERVER -->
<!--LOCAL-->
<add name="AccuFinanceEntities" connectionString="metadata=res://*/AccuFinanceEntities.csdl|res://*/AccuFinanceEntities.ssdl|res://*/AccuFinanceEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=laptop;initial catalog=mydb;Integrated Security=True;MultipleActiveResultSets=True;App=AccuFinance"" providerName="System.Data.EntityClient" />
</connectionStrings>
I think I need to add or remove something from one of these, but not sure which. It worked fine under EF5. Project compiles and builds, and I can edit my EDMX file (I can update model from database).
What am I doing wrong?

EF6.0 Code First - Change connection

After developing EF codefirst application locally,
I copied the database from localdb with generate scripts and create a new one at azure.
I added connection string to config file like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DefaultConnection"
providerName="System.Data.SqlClient"
connectionString="Server=tcp:XXXXXXX.database.windows.net,1433;Database=XXXXXX;User ID=XXXXXXXXXXX;Password=XXXX;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"/>
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
When I try to call an object, I get this exception:
"Invalid object name 'dbo.Banner'."
DB connection is correct. I can connect with SQL Server Management Studio.
What is wrong with this code?
Does the manually creating DB cause the problem?
This problem happen specially when you are restoring database to another server and if you have changed the databse username.
To solve this problem the best way is to change all SCHEMA to dbo.
To change SCHEMA execute following code through your MSSQL Enterpeise manager
ALTER SCHEMA dbo TRANSFER oldSchema.TableName
It seems that the tables has not been created.
Some pointers:
Did you enable Automatic Migration?
AutomaticMigrationsEnabled = true;
You maybe prefer doing a manual update/creation?
In that case, select a project containing the configuration that points to your Azure DB and in the Package Manager console:
update-database
Or you can do it in code as well using the MigrateDatabaseToLatestVersion initializer class.
Here are some additional information for reference:
- MSDN: Code First Migrations
- ASP.NET: Code First Migrations and Deployment with the Entity Framework in an ASP.NET MVC Application

Code-First approach, location of database and all instances (v110, etc)

I'm following this tutorial to create a Code-First Database.
After installing EF and creating those first basic classes it says:
Run your application and you will see that a
MigrationsCodeDemo.BlogContext database is created for you.
Well, I have VS2013 Pro with all the SQL Servers installed (full installation of VS2013 Pro).
Now as you can see from the image below, I can't find my database after running the program as the tutorial is suggesting. When I try to do the migrations part of the tutorial, I indeed get the error it supposed to get, implying that somewhere the database actually has been created. I just am not able to find it.
Where is it located?
Edit: added App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
The physical master database files are at
C:\Users\<user>\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\v11.0
The DB files are simply in C:\Users\<user>
You can also connect to localdb using SSMS by using server (localdb)\v11.0 and delete the database from there.
The official documentation is here
http://msdn.microsoft.com/en-AU/library/hh510202.aspx
Edit by QuantumHive:
I finally figured out that the Database can be found in the instance called MSSqlLocalDb which Entity Framework added by default in my App.config. I now have a visualization of the database in Visual Studio:
I'm guessing those tutorials are old and referring to the v11.0 instance, which perhaps and older version of EF added that by default at the time.
You are connected to localDb\Projects instance and not to localDb\v11.0, the only thing you need to do is clicking 'Add Sql Server' and paste '(localDb)\v11.0' into Server name, then connect and you are done.

Categories