EF4 The provider did not return a Provider Manifest Token string - entity-framework

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.

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.

Exception while deploying ASP.NET Website after Using EF

I have two projects, one containing WS which access to the DB and the other one is front-end.
The two projects are deployed on seperate servers.
Before Using EF and linq for accessing the DB in the WS project, everything was working fine, but after that I got and exception saying "the underlying provider failed on open".
Please ca you help me with that. Thank you in advance.
Here is my ConnectionString (WS Project):
<connectionStrings>
-->
<add name="ConString1" connectionString="Data Source=#address,1433;Initial Catalog = MpCarteCoBrandee;MultipleActiveResultSets=True;Trusted_Connection=True;" providerName="System.Data.SqlClient" />
<!--<add name="ConString" connectionString="Data Source=AOUS-PC;Initial Catalog=TestMobilePayment;Integrated Security=SSPI" providerName="System.Data.SqlClient" />-->
<add name="MobilePaymentEntities" connectionString="metadata=res://*/MobilePayment.csdl|res://*/MobilePayment.ssdl|res://*/MobilePayment.msl;provider=System.Data.SqlClient;provider connection string="data source=#address,1433;initial catalog=MobilePayment;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />

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.

Can not create my DbContext in a Unit test

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!

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.