databaseinitializer is not called, configured via web.config - entity-framework

I'm trying to launch the databaseinitializer but nothing happens. It used to work before I switched from MySql.Data.Entity.EF6 to the new one. I can write anything as the attribute type of context and it will still load the app but the database is never initialized.
Thanks
<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" />
...
<entityFramework>
<contexts>
<context type="GroupeFleury.DAL.ApplicationDbContext, GroupeFleuryApps, Version=1.0.0.0, Culture=neutral">
<databaseInitializer type="GroupeFleury.DAL.DbInitialisateur, GroupeFleuryApps, Version=1.0.0.0, Culture=neutral" />
</context>
</contexts>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"></defaultConnectionFactory>
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.EntityFramework"/>
</providers>

Related

Connection string changed unexpectedly in PostgreSQL code first

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.

Why is Migrations looking for Sql Server Provider?

Here is my 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>
<connectionStrings>
<add name="DataContext" connectionString="Server=localhost;Port=5432;Database=myDatabase;User Id=postgres;Password=password;Preload Reader = true;" providerName="Npgsql" />
</connectionStrings>
<entityFramework>
<providers>
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
</providers>
</entityFramework>
</configuration>
When I run update-database it gives at the end of the stack trace...
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.
Another error I'm running into when I add to the entityFramework section..
<defaultConnectionFactory type="Npgsql.NpgsqlFactory, Npgsql" />
Failed to set Database.DefaultConnectionFactory to an instance of the
'Npgsql.NpgsqlFactory, Npgsql' type as specified in the application
configuration.
What am I missing?
You could try using a DbProvidersFactories instead of the connection string:
<system.data>
<DbProviderFactories>
<remove invariant="Npgsql" />
<add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description=".Net Framework Data Provider for Postgresql" type="Npgsql.NpgsqlFactory, Npgsql" />
</DbProviderFactories>
</system.data>
I got this working by adding Entityframework, Npgsql and Npgsql.Entityframework as three individual installs using Nuget. Then the following 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>
<connectionStrings>
<add name="[Name of EF Context class]" connectionString="Server=localhost;Port=5432;Database=[Name of existing database];User Id=[username];Password=[password];" providerName="Npgsql" />
</connectionStrings>
<entityFramework>
<providers>
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework"></provider>
</providers>
<defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, Npgsql.EntityFramework" />
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="Npgsql" />
<add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description=".Net Framework Data Provider for Postgresql" type="Npgsql.NpgsqlFactory, Npgsql" />
</DbProviderFactories>
</system.data>
</configuration>

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>

How to tell if using EF5

Am taking a Silverlight / WCF RIA Service project from EF4 to EF5. Target is .NET4.5
Here is a spike - am using a DB First approach. ie create edmx, then use the Domain Services Classes Wizard
So looking good so far as have
EntityFramework (5.0.0.0)
Microsoft.ServiceModel.DomainServices.EntityFramework (4.0.0.0) DbDomainService in here
System.Data.Entity (4.0.0.0)
as opposed to old way of:
EntityFramework (5.0.0.0)
System.Data.Entity (4.0.0.0)
System.ServiceModel.DomainServices.EntityFramework (4.0.0.0) LinqToEntitiesDomainService in here
then have put a simple UI on to access the 2 table db and it worked
Problem: This web.config isn't what I expected:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="system.serviceModel">
<section name="domainServices" type="System.ServiceModel.DomainServices.Hosting.DomainServicesSection, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" allowDefinition="MachineToApplication" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web>
<httpModules>
<add name="DomainServiceModule" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</httpModules>
<compilation debug="true" targetFramework="4.5">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
<!-- profile stuff commented out-->
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
<add name="DomainServiceModule" preCondition="managedHandler" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</modules>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=aspnet-BusinessApplication5.Web-20130603151851;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="TestDBEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=TestDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
I was expecting to have to put in:
<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>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Got these bits of config from the following. Perhaps they are all codefirst specific.
https://github.com/jeffhandley/riabooks
http://jeffhandley.com/archive/2012/12/10/RIA-Services-NuGet-Package-Updates-ndash-Including-Support-for-EntityFramework.aspx
http://mcasamento.blogspot.co.uk/2012/10/entity-framework-5-code-first-and-wcf.html
The RIA Services dll is compiled against EF 4, not EF5, so the binding Redirect is used to make everything work. If/when RIA Services is open sourced there will be a new release of RiaServices.EntityFramework that will be compiled against EF 5 and EF 6. One of the benefits of open source will be the ability to put out as many NuGet packages as I want to support all combinations.