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

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.

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!

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

Entity Framewok Code First "ADO.NET provider not found" with local SQL Server CE DLL's

I am writing a C# application which uses SQL Server CE 4.0 files, which are accessed through the Entity Framework 6.0 via code-first. (The application needs to be able to use local dll's for the SQL Server CE connection i.e. the application needs to be XCOPY deployable). The application runs fine on my development machine, but on other machines (e.g. VMs with just Win7 and .NET 4.0), I get an ArgumentException:
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. See the inner exception for details.
The inner exception message says:
Unable to find the requested .Net Framework Data Provider. It may not be installed.
I have searched Google and SO and most of the comments indicate ensuring the App.config file is correct. I believe mine is (default connection factory and provider sections), but here are the contents:
<?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.0" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlServerCe.4.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
</providers>
</entityFramework>
</configuration>
The build folder includes the following files (in addition to the application-specific files, of course):
EntityFramework.dll
EntityFramework.xml
EntityFramework.SqlServer.dll
EntityFramework.SqlServer.xml
EntityFramework.SqlServerCompact.dll
EntityFramework.SqlServerCompact.xml
In each of the amd64 and x86 subfolders are the following files:
sqlceca40.dll
sqlcecompact40.dll
sqlceer40EN.dll
sqlceme40.dll
sqlceqp40.dll
sqlcese40.dll
I am certain the program runs on the development machine because SQL Server CE has been installed, but how do I get it to run using just local SQL Server CE dll's on other machines?
See http://tech.aendeavors.com/2011/06/09/bin-deploy-sqlce-4-0-and-ef-4-1/
It seems the relevant bits you might be missing are:
Make sure System.Data.SqlServerCe is referenced and set to "Copy
local" in properties.
Add the following to app.config:
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SqlServerCe.4.0" />
<add name="Microsoft SQL Server Compact Data Provider 4.0"
invariant="System.Data.SqlServerCe.4.0"
description=".NET Framework Data Provider for Microsoft SQL Server Compact"
type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
</DbProviderFactories>
</system.data>
I was facing the same problem today.
After reinstalling EF several times, what solved my problem was referencing EF in my Console project along with the DAL project (the one that actually uses EF).
If you're using a reference that in turn references EF, you gotta add it to this project as well.
Sorry about the late response - actually, there's a really simple solution:
If you investigate the packages\EntityFramework.6.1.3\lib\net45 folder, you'll notice that there's another dll that's already in there - EntityFramework.SqlServer.
Simply add a reference to that, and everything will work.
I had the same issue and nothing worked for me. The only thing that resolved my this issue for me was to uninstall SQL Server Compact from my machine (via add/remove programs) and re-install it.
In my case, "EntityFramework.SqlServerCompact" package was missing. Just wanted to share if in case it can save someone's day!

How to reset database migrations using EF code first?

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'

Categories