entity framework code first migrations from seperate windows class library - entity-framework

i have a solution that contains an MVC project and windows class library project that uses entity framework and is a data access layer. I tried to enable migrations with the following package manager console line
Enable-Migrations -ProjectName PortlandRoad.DAL -ContextTypeName PortlandRoadDBContext -Force
I try to update the database using the following line
Update-Database -Verbose -Force
if i do this with the mvc project as the startup project it works, but uses the connection string in the mvc project web.config. If i do this with the dAL project as the startup project i get the following error :
A file activation error occurred. The physical file name '\PortlandRoadDB.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.
the app.config file for my DAL project is as follows :
<?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" />
<!--<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="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=PortlandRoadDB;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\PortlandRoadDB.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
can anybody tell me how to correct this so that the migrations work specifically for the dal project and are not dependent on the MCV project being the startup project
thanks

I've the same issue in a WPF project.
I think that you have two options.
Option 1
At startup in you windows project try to specify the correct path to your database using
AppDomain.CurrentDomain.SetData("DataDirectory","your\physical\path");
With this, you will have the same error but your migrations can still being applied to your principal database.
option 2
Find a way to replace dynamically this |DataDirectory| in your app.config file before starting your application. (.bat , or .ps or ...).
another option: The bad one.
Before running your migration change the path to you DataDirectory to a physical temporary one in your app.config (this file will help you keep up to date migrations history)
before launching your application replace the temporary file path by |DataDirectory| and change this parameter in your startup with the App.config
With this work around, if your are planning to deploy your application, you will keep track of all migrations in order to anticipate futures database modifications.
VoilĂ .

Could it be that you're using "|DataDirectory|" in the connection string in the DAL project? Since "|DataDirectory|" is an Asp.Net folder and the DAL project is not an Asp project it probably doesn't recognize that as a directory and it probably doesn't exist thing the DAL project.

Related

Calling Entity frameworks file from Exe, normally a part of a web service

I am working on a older webservice, which uses Entity frameworks 6 to connect to Database.
The project is split up into web-service (controller) and "repository", which operated the database.
The idea is to now create an EXE which calls the repository, hence class library, which uses Entity framework. However, here I have some settings issues with connecting to the database.
The repository's config holds connection data, but for some reason it cannot connect.
Running is a default, hence default setting (none) of my exe, I get this error:
I can debug in into
public partial class My_TestEntities : DbContext
{
public My_TestEntities()
: base("name=My_TestEntities")
{
}
It crashes at that moment.
"Exception has been thrown by the target of an invocation." - which points to something deeper that just this.
I try to copy default into my own app.config
<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="my_TestEntities" connectionString="metadata=res://..... stuff here..." />
</connectionStrings>
<appSettings />
This results in
"The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception."
Copying the full config from the repository, some web stuff etc results in the same
Copying the configuration from the controller results in same as well.
I tried those things as I don't know what to do. I googled and found nothing.
So maybe here I can get some good ideas
I can answer this myself.
The parent exe calling the repository needs to have Entity framework (nuget package) installed, this will add some parts automatically to the config, as above.
Next, only the connection string is needed in the config file.
Then it works

Error 500 adding Entity Framework to web.config on asp.net web api

In an ASP.net 4.5 Web Api, locally and in production environments is working fine.
I added the Nuget package Entity framework 6.1.3 to the solution. Without adding model. Locally with EF it is working, but when I publish to production, it throws "error 500".
EF add lines to the web.config:
<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>
In production, when I remove these lines, it works fine (of course without EF).
I tried adding a model then cause the API throws 500.
Is there any way to use EF in this hosting?
Is a problem in the web.config or in the hosting? The api is in a second level directory of a MVC web app.
pd: The hosting uses Managed Pipeline Mode: Integrated.
More info:
When i GET from browser http://max-prueba.somee.com/api/Position/41
it response a status 500 and Server Error
500 - Internal server error. There is a problem with the resource you
are looking for, and it cannot be displayed.
Next step i add line to web config to view description of the error
<customErrors mode="Off"/>
But the response is the same.

SQlite Entity Framework ADO.net provider error 1.0.92.0

I'm trying to understand how SQlite and entity frameworks interacts.
I create a new fresh console project in my visual studio 2013. I install the nuget packet of SQlite 1.0.92.
I create a new empty model (edmx) and try to populate it from a static example database (such as northwind.db).
After this I get this error: error 0175: the ado.net provider named "System.Data.SQLite.EF6" is not registered on the computer.
Any ideas?
Thank you
Lorenzo
After some tests I found a possible solution. Let's start from the beginning:
Download the SQLite ado.net provider from sqlite server (x86 version).
You have to manually register in the GAC those three libraries: system.data.sqlite, system.data.sqlite.ef6, system.data.sqlite.designer with gacutil
Start visual studio 2013. Add reference to nuget sqlite 1.0.92.0 (This will add reference to entity framework 6.1 too).
Your app.config will look like this:
<?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>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite" />
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
<remove invariant="System.Data.SQLite.EF6" />
<add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".Net Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
</DbProviderFactories>
</system.data>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
</entityFramework>
<connectionStrings>
Add a new Sqlite connection to the db in your server explorer and test it.
Now add and create a new empty ado.net entity model to your project.
This should work... but when your run the application and pick up some records in one of the dbset you will get an exception telling you that there's no ado.net provider registered for your connection (system.data.sqlite).
Go to your config file and add this to you provider section (just copy the previous sqlite line and cut the .ef6 postfix in the invariant name):
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
Save all and rebuild the application. Now it works but if you go back to the model detail and try to refresh the model from the db you will get a provider exception again. Comment the last provider added and retry. IT WORKS!!!
I think it's due to a conflict between provider names in the syste.data.sqlite and system.data.sqlite.ef6
Despite this I can't do model first (project a model and then create a new database from it) because I get another ado.net provider exception.
Any ideas about that?
Thank you
Lorenzo
_________________ UPDATE 04-2014 _________________
After some testing and googling...
I read that when you want to use the VS design tools you have to install the right x86 version of the SQLite drivers. These drivers, to work properly with the VS tools, needs to register their version of the System.Data.SQLite in the GAC during the installation process (only after this VS knows where to find the connection provider for the design tools).
When you use the model-first approach and you ask the model to create the db instead of use the correct driver registered in you app.config it tryes to open the entity connection with the one registered in GAC and used by the visualmodel creation tool (and wich is not compatible). I think there's no way at the moment to use the VS's entity modelling tool with the SQLite provider.
Lorenzo
#LoxLox : I don't think you have to register dll with GAC. I have same problem like yours but I just need to edit the .config file as discussed in this post by adding .EF6 suffix to invariant.

Entity Framework 5 Code Migration doesn't create database

I'm trying to use Entity Framework 5 Code First.
I've created model classes and context.
After that following Microsoft instructions I've enabled code migrations.
Than I skipped running my application (during which DB should be created first time) and use Add-Migration command. Migration was generated successfully.
Update-Database call was also successfull. But I'm not seeing my DB at all. It is absent!
SQL Management Studio and Visual Studio Server Explorer show only 4 default system databases and that's all!
I also tried to launch my application - it doesn't change anything.
I'm using
public MyContext() : base("name=MyContext") { }
such type of constructor (so I need specify connection string with MyContext name.
Here is my app.config example:
<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.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="BettyContext" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=MyDB.sdf"/>
</connectionStrings>
</configuration>
I have no idea what's going on. Seems that no one has such problem. May be I've missed some evident thing, but howbeit I'm waiting for some help.
Do you see an exception? If you don't see an exception you probably are not looking at the right database. You can try adding some data to the database with your app and then read it. If the data can be successfully added and read and you still don't see the database it would confirm that you are looking at wrong database.

'compilerVersion' attribute in the provider options must be 'v4.0' or later

Getting this error:
The value for the 'compilerVersion'
attribute in the provider options must
be 'v4.0' or later if you are
compiling for version 4.0 or later of
the .NET Framework. To compile this
Web application for version 3.5 or
earlier of the .NET Framework, remove
the 'targetFramework' attribute from
the element of the
Web.config file.
It was working on our dev system, and we are now deploying to QA using an xcopy type deploy.
We don't have a "compilerVersion" anywhere in the web.config, and the targetFramework is set to "4.0". We have done an IISReset.
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</compilation>
UPDATE 2: when we removed the entire section, we got past the error. So my question is, what is wrong with leaving in this section if we are on .NET 4.0?
We just did a full xcopy of exactly what was in Test to QA, and in QA it gets the error and DEV doesn't. So the software and configs are identical. Why would it work on one server and not another? IIS options seem to be identical.
taken from: http://www.asp.net/whitepapers/aspnet4/breaking-changes#0.1__Toc256770150
you should go to your server root web.config and include the whole
<system.codedom>...</system.codedom>
tag content into a
<location path="" inheritInChildApplications="false">...</location>
tag
Possible solution here - asp.net/whitepapers/aspnet4/breaking-changes#0.1__Toc256770150
After fighting with this for a while, I ended up creating a new IIS site using a different port and adding my ASP.Net 3.5 application to the new site and it is working perfectly.
I got the same error and found out that my v4.0 website was hosted under the default website that was v2.0. If you've such setup then you need to remove the following line from your web.config:
<compilation defaultLanguage="c#" debug="true" targetFramework="4.0"/>
I hope that helps.