Connection string changed unexpectedly in PostgreSQL code first - postgresql

1- Installed Npgsql 3.1.9.0 and EntityFramework6.Npgsql.dll 31.0.0 dlls
2- Defined a DbContext as following
public class MyDbContext : DbContext
{
public MyDbContext()
: base("myConnectionString")
{
}
public virtual DbSet<Tag> Tags { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("public");
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
3- And my app.config :
<configuration>
<connectionStrings>
<add name="myConnectionString" providerName="Npgsql" connectionString="Host=localhost;Port=5432;Database=mv_test;User Id=postgres;Password=devel;" />
</connectionStrings>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v13.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
</providers>
</entityFramework>
</configuration>
4- My test snipped code
MyDbContext myContext = new MyDbContext();
int c = myContext.Tags.Count();
the exception :
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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
but I can see the connection is default, is that connection string is not correct or something else?
UPDATE:
After many tries and Installing Npgsql again and again I knew that <configSections> must be the first child of the root <configuration>, corrected it and run again hope to run properly but at the end I have got this exception:
Could not load file or assembly 'Npgsql, Version=3.1.2.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
I think some dependensies need to be referenced but I am newbie with Npgsl can anybody knows what went wrong.

YES, No pain No gain , There is no any barrier for a developer!
After many problems with Npgsql and code first I get succeeded to run my snipped code but here some notes that my useful for someone
As they said by installing Install-Package EntityFramework6.Npgsql by Nuget , Npgsql and all it's dependencies will be referenced without any problem but I have got many fails so by installing Install-Package EntityFramework6.Npgsql the following libraries be referenced as desc given bellow (Tuesday, November 22, 2016):
1- Npgsql version 3.1.0.0
2-EntityFramework6.Npgsql version 3.1.1.0
3-EntityFramework and EntityFramework.SqlSrver version 6.0.0.0
after establishing DbContext and data model you may get exception that said
Could not load file or assembly 'Npgsql, Version=3.1.2.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
because Ngpsql is not compatible with Entityframework6.Npgsql or another reason should be located Ngpsql located in C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Npgsql\v4.0_3.1.9.0__5d8b90d52f46fda7 is 3.1.9
but to bypass this exception I have added Npgsql version 3.1.9.0 manually then code runs like a charm.
so the App.config should like:
<?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="myConnectionString" providerName="Npgsql" connectionString="Host=localhost;Port=5432;Database=mv_test;User Id=postgres;Password=devel;" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v13.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
</providers>
</entityFramework>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Npgsql" publicKeyToken="5d8b90d52f46fda7" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
hope this be useful for you.

Related

EF Code First (Azure) - An error occurred accessing the database

Until recently my solution only had 1 web project. It is deployed to Azure.
There then became a requirement for WebJobs, which should share the data layer, so I extracted out any EntityFramework code (Configuration/IdentityModels) into their own project.
Now when I try to use the command line to "add-migration" I get the following error:
An error occurred accessing the database. This usually means that the
connection to the database failed. Check that the connection string is
correct and that the appropriate DbContext constructor is being used
to specify it or find it in the application's config file. See
http://go.microsoft.com/fwlink/?LinkId=386386 for information on
DbContext and connections. See the inner exception for details of the
failure.
Here is what the App.Config file looks like for the EF project:
<?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" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></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>
<connectionStrings>
<add name="Default" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Sample;Integrated Security=True;Connect timeout=15;Encrypt=False;TrustServerCertificate=False" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Can anyone help please?
I just had the same problem.
It seems there is a required order of configuration child elements.
In my case I changed them to:
<configSections>...</configSections>
<startup>...</startup>
<system.data>...</system.data>
<connectionStrings>...</connectionStrings>
<entityFramework>...</entityFramework>

EF 6.1 Configuration using Interceptors with WCF

I am trying to turn on database logging using the interceptor feature that was added in EF 6.1. It works fine when I turn on in a ASP.NET web.config file, but when I trying to use in a WCF service that uses EF 6.1, it gets the following error
Exception: System.ServiceModel.ServiceActivationException: The service '/DataAccessService.svc' cannot be activated due to an exception during compilation. The exception message is: The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception..The more telling message is from the inner exception: Unrecognized element 'interceptors'. web.config line 123)
NOTE: based on the first answer, I am providing more elements from the web.config
<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>
<interceptors>
<interceptor type="System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework">
<parameters>
<parameter value="C:\tmp\DataAccessLogOutput.txt" />
</parameters>
</interceptor>
</interceptors>
</entityFramework>
You didn't register the Entity Frame Work configuration section that defines <interceptors> correctly. In other words do you have this at the top of your 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.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
</configuration>

unable to create entity framework database in class library

I have a class library. within this project I have a folder named 'models' with a few class models. I have all the references I need.
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>
<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>
<connectionStrings>
<add name="DefaultConnection2" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=MDD;AttachDbFilename=|DataDirectory|\MDD.mdf;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
After running update-database it throws the following error:
A file activation error occurred. The physical file name '\MDD.mdf'
may be incorrect. Diagnose and correct additional errors, and retry
the operation. CREATE DATABASE failed. Some file names listed could
not be created. Check related errors.
solved.
the app.config does not suppose to have <connectionStrings>. the Web.config do.
then in the MyDbContext.cs:
public MyDbContext():base("connectionString"){}

Unable to find the requested .Net Framework Data Provider. It may not be installed

This morning I suddenly got this exception when trying to add a custom migration step to our EF Code-first migrations:
The ADO.NET provider with invariant name '.NET Framework Data Provider for SQL Server' is either not registered in the machine or application config file, or could not be loaded.
I have no problem executing a update-database against our SQL Server but the add-migration command fails.
We are using.Net 4.5.1 with EF Code First 6.0.1 against SQL Server 2012.
Our configuration file for our data project is 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>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlClient" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="Data.StorageContext" connectionString="server=.\MSSQL01;Database=OutageTracking; Integrated Security=True; MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
</startup>
</configuration>
Note that Visual Studio 2012 complains about the -element in the -section.
Any thoughts?
Thanks,
/Jesper
".NET Framework Data Provider for SQL Server" is not an invariant name, so some .config file somewhere must be in a bad state

ASP.NET MVC 4 + Entity Framework 6 + SQL Compact Edition 4.0 deployment no install

Im trying to deploy a basic MVC 4 app that uses Entity Framework 6 (Code First) and SQL Compact 4.0
on a clean install of windows 7 with iis installed (mvc 4 and sql compact 4.0 is not installed).
I got the following nuget packages installed on the db project:
EntityFramework (Version 6.0.0-alpha2)
EntityFramework.SqlServerCompact (Version 6.0.0-alpha2)
Microsoft.AspNet.Providers (Version 1.2)
Microsoft.AspNet.Providers.Core (Version 1.2)
Microsoft.SqlServer.Compact (Version 4.0.8876.1)
In web.config i got the following config:
<connectionStrings>
<add name="[Name]" connectionString="Data Source=|DataDirectory|[FileName].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>
<providers>
<provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</providers>
</entityFramework>
Bin folder (sql ce files are in amd64 and x86 folder:
[amd64]
[x86]
Antlr3.Runtime.dll
DotNetOpenAuth.AspNet.dll
DotNetOpenAuth.Core.dll
DotNetOpenAuth.OAuth.Consumer.dll
DotNetOpenAuth.OAuth.dll
DotNetOpenAuth.OpenId.dll
DotNetOpenAuth.OpenId.RelyingParty.dll
EntityFramework.dll
EntityFramework.SqlServer.dll
EntityFramework.SqlServer.xml
EntityFramework.SqlServerCompact.dll
EntityFramework.SqlServerCompact.xml
EntityFramework.xml
Microsoft.Web.Infrastructure.dll
Microsoft.Web.WebPages.OAuth.dll
Newtonsoft.Json.dll
ShipMedProto.DB.dll
ShipMedProto.DB.pdb
ShipMedProto.Web.dll
ShipMedProto.Web.pdb
System.ComponentModel.DataAnnotations.dll
System.Data.DataSetExtensions.dll
System.Data.SqlServerCe.dll
System.Data.SqlServerCe.Entity.dll
System.Net.Http.Formatting.dll
System.Web.Helpers.dll
System.Web.Http.dll
System.Web.Http.WebHost.dll
System.Web.Mvc.dll
System.Web.Optimization.dll
System.Web.Providers.dll
System.Web.Razor.dll
System.Web.WebPages.Deployment.dll
System.Web.WebPages.dll
System.Web.WebPages.Razor.dll
WebActivatorEx.dll
WebGrease.dll
WebMatrix.Data.dll
WebMatrix.WebData.dll
When I deploy to the clean machine I get the following error message:
Unable to find the requested .Net Framework Data Provider. It may not be installed.
On my dev machine it is working fine. (I got sql ce installed)
Any clue what Im doing wrong?
I think what you really want to do is manage the NuGet packages such that you add SqlServerCompact support for EntityFramework. Just right click your references and Manager NuGet packages. Then in the dialog do a search for SqlServerCompact. You SHOULD see this:
Adding this should add the equivalent strings that were in the answer above. I ended up with this in the web.config (or app.config if you're doing an app)
<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>
<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>
Sorry, I'm about a year late but hopefully this helps someone else.
This is a configuration I am using that works.
I've added the projects from NuGet:
EntityFramework (6.0.1)
EntityFramework.SqlServerCompact (6.0.1)
Microsoft Sql Server Compact Edition (4.0.8876.1). Note, I do not have this assembly flagged to copy local.
I've added the files from the SQLCE 4 private install folders
And then I set my config as follows:
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="SecureSend.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
<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>
<runtime>
<loadFromRemoteSources enabled="true" />
<ThrowUnobservedTaskExceptions enabled="true" />
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="bin"/>
<dependentAssembly>
<assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Data.SqlServerCe" publicKeyToken="89845dcd8080cc91" />
<bindingRedirect oldVersion="0.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<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>
<connectionStrings>
<add name="LocalUserContext" connectionString="Data Source=c:\temp\LocalUserDataStore.sdf" providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>
<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>
</configuration>