Can not create my DbContext in a Unit test - entity-framework

When I create my TLPContext in a unit test I get an exception at the bottom of this page.
I did this in the unit test:
DbContext context = new TLPContext();
context.Database.CreateIfNotExists();
Is this not the correct approach?
What is wrong with my connection string / provider ?
I have followed this: http://www.thereforesystems.com/turn-on-msdtc-windows-7/
but it did not help after a reboot.
Thats my app.config file in my unit test project:
<?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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
<connectionStrings>
<add name="TLPContext" providerName="System.Data.SqlClient" connectionString="Data Source=LISA\SQLEXPRESS;Initial Catalog=TLPTEST;Integrated Security=True;Pooling=False;"/>
</connectionStrings>
</configuration>
That is my DbContext, it should be enough to create the db but not the tables...
public class TLPContext : DbContext
{
}
That is the exception I get:
when I create a TLPContext instance in my unit test SetUp:
Test 'TLP.DataAccess.UnitTests.GenericRepositoryTests.Test' failed: System.Data.ProviderIncompatibleException : An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.
---- System.Data.ProviderIncompatibleException : Der Anbieter hat keine ProviderManifestToken-Zeichenfolge zurückgegeben.
-------- System.Data.SqlClient.SqlException : MSDTC on server 'LISA\SQLEXPRESS' is unavailable.
at System.Data.Entity.ModelConfiguration.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection)
at System.Data.Entity.ModelConfiguration.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest)

If you wouldn't use a connection string, it would automatically find the database server (it searches for SQLEXPRESS) and create the database based on your project's name.
What you are missing is giving the connection string or its name, which can be achieved by using one of the DbContext's constructor:
TLPContext(string connectionStringOrName) : DbContext(connectionStringOrName) {}
I hope this helps!

Related

EntityFramework connect to Visual Studio SQL Server (localdb)

I am running a local instance of Sql Server through Visual Studio:
It is online and I am able to view the data in the database through the UI.
My App.config file looks like the following:
<?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="EntitiesContext" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" providerName="System.Data.SqlClient"/>
<add name="DefaultConnection" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" 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>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/></startup>
</configuration>
And my EntitiesContext looks like the following:
Could someone please help with what I am missing as this keeps failing connection with the following:
System.Data.SqlClient.SqlException: 'A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)'
Inner Exception
Win32Exception: The system cannot find the file specified
The issue is that the connection string you are using is reverting to a named pipe connection, and it's telling you that nothing is listening on that Pipe.
Changing it to "localhost\MSSQLLocalDB;...." should work if your local database is set up for Shared Memory connections.
To check what protocols are enabled on your server you can run MMC.exe then add the plugin for SQL Server Configuration Manager. Under SQL Server Network Configuration it will show whether your SQL Server instance is allowing Named Pipes or TCP/IP. I believe by default SQL Express only enables Shared Memory which is fine for local testing purposes.

How can I make Quartz.NET AdoJobStore work with Entity Framework?

.NET 4.51
Entity Framework 6.x
I have a separate project / assembly that wraps the core Quartz.NET functionality. I want to programatically configure Quartz.NET to persist information to SQL Server. As this is a separate assembly the App.config is very minimal and I want to pass in a connection string. Here is the complete 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>
<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>
and here is the code I am using to configure Quartz.NET:
properties["quartz.scheduler.instanceName"] = "My Scheduler";
properties["quartz.scheduler.instanceId"] = "MySchedulerId";
properties["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz";
properties["quartz.jobStore.useProperties"] = "true";
properties["quartz.jobStore.dataSource"] = "default";
properties["quartz.dataSource.default.connectionString"] = aConnectionString;
properties["quartz.dataSource.default.provider"] = "System.Data.SqlClient";
properties["quartz.jobStore.tablePrefix"] = "QRTZ_";
The value for aConnectionString is:
TESTING
"Data Source=.\\SQLEXPRESS;Initial Catalog=xxxxx;Integrated Security=True;MultipleActiveResultSets=True"
PRODUCTION
server=sql.server.com;User Id=xxxx;password=xxx;Persist Security Info=True;database=xxxx" providerName="System.Data.SqlClient
However when I attempt to call GetScheduler() I receive the following error message:
Could not Initialize DataSource: default
and the inner exception has:
{"There is no metadata information for provider 'System.Data.SqlClient'\r\nParameter name: providerName"}
So what am I missing here? What do I have to change to make this work?
Quartz does not support plugging in Entity Framework infrastructure, it works without external frameworks. Quartz's provider is different from ADO.NET's provider infrastructure. You can see an programmatic configuration example for AdoJobStore in Example 13. So the correct value would be SqlServer-20.
You can also configure the connection string by providing parameter connectionStringName and Quartz will look for that named connection string from connectionStrings section.

EF4 The provider did not return a Provider Manifest Token string

Yes, one more question about Provider manifest token. Unfortunately all previous 22 questions was not useful to solve my problem. I developing simple web application using MVC4 + Code First + Sql Express.
Here is my context descendant:
public class MCQContext : DbContext
{
public MCQContext()
: base("name=ApplicationConnection")
{
}
...............
}
And here - part of web.config related to the problem:
<configuration>
<configSections>
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false" />
</configSections>
<connectionStrings>
<add name="ApplicationConnection"
connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="name=ApplicationConnection" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
So, as you can see, correct connection string passed to base of context class (I got the same error if I rename connection string to "MCQContext" and do not pass anything to parent context class).
I have no idea how to fix it. This behavior reproduced if I creating absolutely empty MVC4 application, remove all packages (I prefer manually specify required assemblies and do not use NuGet) and fix references (including reference to sqlserver express).
The problem with your connection string here is:
<add name="TrempimModel"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|aspnetdb.sdf;
User Instance=true"
providerName="System.Data.SqlClient" />
You're basically defining what "server" you're connecting to - but you're not saying what database inside the file to connect to. Also - the file extension for SQL Server Express database files is .mdf (not .sdf - that's SQL Server Compact Edition) - you need to take that into account, too! (was a typo, according to comment by OP).
You need to define an extra database=.... (or Initial Catalog=.....) in your connection string:
<add name="TrempimModel"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;
database=YourDatabaseName;
AttachDBFilename=|DataDirectory|aspnetdb.mdf;
User Instance=true"
providerName="System.Data.SqlClient" />
Then it should work just fine.

ADO.NET requesting existing connection string

I created an ADO.NET object in my WCF service project, but when I try to connect in it I get this error:
No connection string named 'FileEntities' could be found in the
application config file.
Here is my code:
public File GetFile()
{
using (FileEntities ctx = new FileEntities())
{
File aFile = (from f in ctx.Files
where f.ID == 5
select f).FirstOrDefault(); // No connection string named 'FileEntities' could be found in the
application config file.
return aFile;
}
}
Here is the App.Config for my service project:
<?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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="FileEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=HOOVERS-PC;initial catalog=Interview_MicahHoover;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
I have tried removing the "name=" prefix in the model.
Ah, the problem was my service project just builds a library. The project that uses the library doesn't have the connection string in it. I added the connection string to that project and it (mostly) works.
Keeping things like this straight is a reason to not break things up into a lot of projects, but the WCF O'Reilly book recommends it.

Entity Framework Migrations: error on web server only

error 0175: The specified store provider cannot be found in the configuration, or is not valid
I am using Entity Framework Code-First Migrations and everything is great locally.
My DB is SQLServer 2008. (Also using SQL CE for local development)
I can use the live SQL Server db connection string locally with no problem.
Here is the Migration code in _appStart.cshtml:
#using EF = System.Data.Entity;
#using TCommon = Told.Web.Common.Logic;
#{
// Manually initialize
var initializer = new EF.MigrateDatabaseToLatestVersion<TCommon.CommonContext, TCommon.Migrations.Configuration>();
System.Data.Entity.Database.SetInitializer(initializer);
initializer.InitializeDatabase(new TCommon.CommonContext());
// DB Maintenance
TCommon.AppUserLogic.CleanDatabase();
// Asp.Net
WebSecurity.InitializeDatabaseConnection("ToldDB", "UserProfile", "UserId", "Email", true);
// Common
TCommon.AppStart.Run();
}
This works great locally. Either against a SQL CE or against my live database. It ran fine locally to migrate my live database to the latest version.
However, when I put it on my web server, it throws this exception:
Exception
Server Error in '/' Application.
Schema specified is not valid. Errors:
(0,0) : error 0175: The specified store provider cannot be found in the configuration, or is not valid.
[MetadataException: Schema specified is not valid. Errors:
(0,0) : error 0175: The specified store provider cannot be found in the configuration, or is not valid.]
System.Data.Metadata.Edm.Loader.ThrowOnNonWarningErrors() +8566285
System.Data.Metadata.Edm.Loader.LoadItems(IEnumerable`1 xmlReaders, IEnumerable`1 sourceFilePaths) +181
System.Data.Metadata.Edm.StoreItemCollection.Init(IEnumerable`1 xmlReaders, IEnumerable`1 filePaths, Boolean throwOnError, DbProviderManifest& providerManifest, DbProviderFactory& providerFactory, String& providerManifestToken, Memoizer`2& cachedCTypeFunction) +211
System.Data.Metadata.Edm.StoreItemCollection..ctor(IEnumerable`1 xmlReaders) +295
System.Data.Entity.Migrations.Extensions.XDocumentExtensions.GetStoreItemCollection(XDocument model, DbProviderInfo& providerInfo) +180
System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel, XDocument targetModel, String connectionString) +48
System.Data.Entity.Migrations.DbMigrator.IsModelOutOfDate(XDocument model, DbMigration lastMigration) +55
System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) +269
System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +456
System.Data.Entity.MigrateDatabaseToLatestVersion`2.InitializeDatabase(TContext context) +64
Now, this has something to do with the migration attempt. So when I wrap the migration call in a try-catch block, I can use the database fine (because I migrated the live db locally).
#using EF = System.Data.Entity;
#using TCommon = Told.Web.Common.Logic;
#{
// Manually initialize
var initializer = new EF.MigrateDatabaseToLatestVersion<TCommon.CommonContext, TCommon.Migrations.Configuration>();
System.Data.Entity.Database.SetInitializer(initializer);
try
{
initializer.InitializeDatabase(new TCommon.CommonContext());
}
catch (Exception ex)
{
}
var db = new TCommon.CommonContext();
var test = db.AppUsers.FirstOrDefault();
// DB Maintenance
TCommon.AppUserLogic.CleanDatabase();
// Asp.Net
WebSecurity.InitializeDatabaseConnection("ToldDB", "UserProfile", "UserId", "Email", true);
// Common
TCommon.AppStart.Run();
}
What would be causing the migration diff-check to fail when I try to run it on my web server?
Edit:
Here is the web.config for the Entity Framework:
<configuration>
<configSections>
...
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</configSections>
...
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
</entityFramework>
<system.web>
<trace enabled="true" requestLimit="40" localOnly="false" pageOutput="true" />
<customErrors mode="Off"/>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
</system.web>
...
<connectionStrings>
<add name="ToldDB" connectionString="..." providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
I have now found the answer:
Make sure that you install these nuget-packages:
Microsoft.SqlServer.Compact and
EntityFramework.SqlServerCompact
Deploy again, and make sure that SQL Server CE is now in the \bin-folder (the folders x86 and amd64 with all the files).
And also these files must be in the bin-folder:
System.Data.SqlServerCe.dll
System.Data.SqlServerCe.Entity.dll
Make sure it is the right version. The ones that are installed with SQL Ce did not work, it must be the ones from the nuget package.
Also make sure the Web.config contains this:
<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>
And this:
<dependentAssembly>
<assemblyIdentity name="System.Data" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
Note that you have to do this no matter if you use SqlCE on the production server. I am not sure you need the bin-files for the database itself, though.