Having trouble with LinqPad Adding a Connection to Entity Framework - linqpad

I am new to Entity Framework and Linq to Entities and I want to try LinqPad but I can't figure out how to make a connection to the edmx model I have created.
I have an MVC project and I added an ADO.Net Entity Data Model against a SQL Sever database ( a Development server, not one on my local machine) Did a Build.Right click in my designer surface and Add Code Generation Item. That has given me two .tt folders, one for my dbContext class, one with all my classes.
Open LinqPad click on Add Connection. Point to the .dll file in my solutions bin folder, then in the Full Type Name of dbContext I choose the entity I created. Now is whee I am having trouble making this work. I point to the solution web.config file and when I click the Test button I get an error saying "Could not load file or assembly 'Entity Framework version=..." & "The system cannot find the file specified. (C:\users..\web.config line 9" Any ideas?

I took configSections away from config file and connection works.
<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.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlServerCe.4.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>

I've had this exact problem with LinqPad 4.42.01. My project is a web project using an EF5 data assembly. I ended up creating a linqPad.config file (below) in the web projectMy and it works great.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<remove name="MySqlServer"/>
<add name="MySqlServer" connectionString="Data Source=mydb;Initial Catalog=mycat;User ID=iamuser;Password=soopersekret;Trusted_Connection=False" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

Related

how to deploy npgsql on Raspberry pi 2 (Mono 4 + ARM proc)?

I use Visual studio 2013 (Windows) to build a small .NET 4.5 application using Npgsql and Entity Framework 6.
On windows it just works fine.
But on Raspbian, the app crash saying it cannot find npgsql provider.
Unhandled Exception:
System.Configuration.ConfigurationErrorsException: Failed to find or load the registered .Net Framework Data Provider 'Npgsql'.
at System.Data.Common.DbProviderFactories.GetFactory (System.String providerInvariantName) [0x00000] in <filename unknown>:0
[...]
On my raspberry pi 2.
I have manually installed Mono v4.0.2
I have copied my app including:
npgsql.dll v2.2.5.0
npgsql.entityframework same version
mono.security v4.0.2
and my app.config contains :
<?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>
<providers>
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
</providers>
</entityFramework>
<connectionStrings>
<add name="minitestEntities" connectionString="metadata=res://*/Model2.csdl|res://*/Model2.ssdl|res://*/Model2.msl;provider=Npgsql;provider connection string="PORT=5432;TIMEOUT=15;POOLING=True;MINPOOLSIZE=1;MAXPOOLSIZE=20;COMMANDTIMEOUT=20;COMPATIBLE=2.2.5.0;DATABASE=minitest;HOST=10.0.0.1;INTEGRATED SECURITY=True;PASSWORD=azerty;USER ID=theuser"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
So i'm wondering if npgsql librairies are correctly loaded, or a if a reference is missing.
I found no docs on how to use npgsql on recent mono + ARM environment.
Any info about this would be really appreciated.
ok i found the solution in an other discution. In the meanwhile i forgot to note its URL so here the answer :
We have to add manually the provider description into app.config :
<system.data>
<DbProviderFactories>
<add name="Npgsql Data Provider"
invariant="Npgsql"
support="FF"
description=".Net Framework Data Provider for Postgresql Server"
type="Npgsql.NpgsqlFactory, Npgsql, Version=2.2.5.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
</DbProviderFactories>
</system.data>
The version and PuclicKeyToken MUST match with npgsql.dll.
To get the public key token, you can run that code :
typeof(Npgsql.NpgsqlFactory).AssemblyQualifiedName
Now i have no idea why this step is not necessary into windows, but a have to do into linux...

How to publish an MVC 6 website in IIS with multiple apps in the src folder

When I try to publish an asp.net 5 website with multiple web apps in the 'src' folder. It works okay in klr but If I run it under IIS, I get this error:
Failed to resolve the following dependencies for target framework 'Asp.Net,Version=v5.0'
Here is a very basic simple project attached that you might want to run under IIS.
http://www.filedropper.com/iissampleapp
Update: updating my question as requested.
In the attached project, I just right click on "IISSampleApp" and click publish. Visual studio creates files on local file system, which I then try to run under IIS. After publish, both of the web apps reside under src folder as src\IISSampleApp and src\SubApp. The IISSampleApp is the main app that should run as website, and it just references a class DateClass from SubApp.
I have created this sample project to get the answer, my actual project have similar structure and I get the same error.
I have uploaded the Precompiled version of the project here: http://www.filedropper.com/precompilediissample
Below is my generated web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="kpm-package-path" value="..\approot\packages" />
<add key="bootstrapper-version" value="1.0.0-beta3" />
<add key="runtime-path" value="..\approot\packages" />
<add key="kre-version" value="1.0.0-beta3" />
<add key="kre-clr" value="clr" />
<add key="kre-app-base" value="..\approot\src\IISSampleApp" />
</appSettings>
</configuration>
edit the web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="kpm-package-path" value="..\approot\packages" />
<add key="bootstrapper-version" value="1.0.0-beta3" />
<add key="runtime-path" value="YOUR-RUNTIME-BASE-PATH\.k\runtimes" />
<add key="kre-version" value="1.0.0-beta3" />
<add key="kre-clr" value="clr" />
<add key="kre-app-base" value="..\approot\src\YOURPROJECT" />
</appSettings>
</configuration>
and set the security policy for \approotand \wwwroot
update: updating the answer after question's update
That just can't work like this because the kpm bundle command packages website as website and not as class library and you have 2 Startup class which is no good. If you want to share code between 2 website create a class library.

"Ado.Net Entity Data Model" template with VS2010, EF6 and SQLite EF6 provider?

As I got a pitch for future project specifying XP compatibility I'm trying to test combination of VS 2010 (WinXP SP3), .Net 4.0.3 (later WPF for UI), Entity Framework 6 (as I found out it should be compatible with .Net 4) and SQLite (System.Data.SQLite / .EF6).
I installed "EntityFramework" and "System.Data.SQLite EF6" packages from NuGet.
("System.Data.SQLite EF6" in turn installed System.Data.SQLite, System.Data.SQLite.EF6 and SQLite.Interop.dll)
Now trying to use "Ado.Net Entity Data Model" template, which I guessed from older tutorials would be next step I get to new connection dialog and no SQLite provider is listed there to select.
I checked my App.config and SQLite data providers are listed there. (If correctly I can't tell)
Looking 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>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0.3,Profile=Client" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<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>
<system.data>
<DbProviderFactories>
<remove invariant="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" />
<add name="SQLite Data Provider"
invariant="System.Data.SQLite"
description=".Net Framework Data Provider for SQLite"
type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data>
</configuration>
The app (empty console app) compiles without errors and runs (doing of course nothing).
I am pretty green with EF + SQLite so can you please advice ?
Am I completely on a wrong track with something ?
So after another several hours of desperate googling I found a hint in an old tutorial about Visual Studio Design Time Component(s).
Apparently I was missing the Design Time Components and also System.Data.SQLite libs installed in GAC, without which VS2010 wizards don't 'see' the SQLite provider.
So despite reading in numerous official places that Setup install package is not a recommended method of getting SQLite libs into the project, it is actually the only way of making VS (2010, 12, ?13?) tools work with it.
Istalled one of "setup packages" from http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki . In my case "Setups for 32-bit Windows (.NET Framework 4.0)".
The Template now offers SQLite as data provider.

LinqPad - EntityFramework 4.3.1 - MissingMethodException - Sql Compact

I have my POCO model is seperate dll than my asp.net mvc 3 website.
I created new connection in LinqPad Beta: v4.41.01 selected option for Poco, provided path to custom dll, Chose DbContext, Chose via parameterless constructor.
This did not work, as I am using SqlServer Compact.
Then I created dummy.config file as follows:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlServerCe.4.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
<connectionStrings>
<add name="ModelContext"
connectionString="data source=database.sdf"
providerName="system.data.sqlserverce.4.0" />
</connectionStrings>
</configuration>
With this, Test on connection is shown successful. Even, tree on left is also populated. But when I run any query, it throws exception as follows:
Method not found: 'System.Data.Entity.DbSet`1 InventoryModelContext.get_xxx()'.
EDIT: Also, note that this seems to be compilation error!
Any solution or workaround? Am I missing something?
Thanks in advance.
I had this problem as well, it started without any apparent reason - did not change anything in my environment.
I am using EF4.2 Poco, LINQPad 4.42.01
To resolve I started running LINQPad as Administartor, it solved it. I am not sure what has changed that made it break, I have not used LINQPad for a while, so maybe it was some auto-update of LINQPad or something like that.
Update: After reinstalling, for some reason the Administartor trick was not working, a new method I found is query first without specifying a database (at the top right, it says <None>), then i get an error that what I am querying for does not exists in this context, and then I set the database to the correct context, and it works.
Still not sure what is actually causing it.

Migrations (Entity Framework 4.3) - script doesn't get executed

The scripts are generated correctly, i use multiple layers so the following commands are run into the Package Manager Console
Eg. When i added Email column to the table : "Owner"
Add-Migration AddEmailToOwner -StartupProjectName "FacturatieMVCv2.Data" -Verbose
--> Result:
Using NuGet project 'FacturatieMVCv2.Data'.
Target database is: 'FacturatieMVCv2.Data.Website.MyResellerContext' (DataSource: V-File \SQLEXPRESS, Provider: System.Data.SqlClient, Origin: Convention).
Next command i use is:
Update-Database -StartupProjectName "FacturatieMVCv2.Data" -Verbose
--> Result:
Using NuGet project 'FacturatieMVCv2.Data'.
Target database is: 'FacturatieMVCv2.Data.Website.MyResellerContext' (DataSource: V-FILE\SQLEXPRESS, Provider: System.Data.SqlClient, Origin: Convention).
Applying explicit migrations: [201203080906140_AddDamnToOwner].
Applying explicit migration: 201203080906140_AddDamnToOwner.
ALTER TABLE [Owners] ADD [Damn] [nvarchar](max)
[Inserting migration history record]
My connectionstring in the App.Config is :
<parameter value="Server=V-FILE\SQLEXPRESS;Persist Security Info=True;Initial Catalog=ProductionReseller;uid=sa;password=n*****3;" />
Although i specified the Initial catalog, it is targetting the wrong catalog. It's created / updated the database table named: FacturatieMVCv2.Data.Website.MyResellerContext .
Following is my 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=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<!--<parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />-->
<parameter value="Server=V-FILE02\SQLEXPRESS;Persist Security Info=True;Initial Catalog=ProductionReseller;uid=sa;password=12345678;" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
In short, the Update-Database command, updated the wrong database.
Using the following command:
Update-Database -StartupProjectName "FacturatieMVCv2.Data" -Verbose -ConnectionString "Server=V-FILE\SQLEXPRESS;Persist Security Info=True;Initial Catalog=ProductionReseller;uid=mydbuser;password=mydbpass;" -ConnectionProviderName "System.Data.SqlClient"
Fixed my problem.
SqlConnectionFactory adds the Initial Catalog to the value you provide. If you want to use a specific connection string for a context, you should add an entry in the connectionStrings section instead. E.g.
<connectionStrings>
<add name="MyResellerContext" connectionString="Server=V-FILE\SQLEXPRESS;Persist Security Info=True;Initial Catalog=ProductionReseller;uid=sa;password=12345678;" providerName="System.Data.SqlClient" />
</connectionStrings>
We store config variables in build configs to switch between these and ran into this issue; the solution was to Clean then explicitly Build.
To expand on that:
Our Db class (the one that inherits DbContext) has a Constructor like:
public Db()
#if AltDb
: base("AltDb")
#else
: base("Db")
#endif
{
}
So if the AltDb build var is defined, it's going to use the ConnectionString named AltDb; otherwise it uses the one named Db. In our Project configs, we have one named AltDb and all it does is define the AltDb build variable.
So we have those 2 connection strings defined in Web.Config:
<connectionStrings>
<add name="Db" connectionString="Initial Catalog=MyProj;Data Source=.\SQLEXPRESS...
<add name="AltDb" connectionString="Initial Catalog=Staging;Data Source...
So, when we want to update the alternate db, we SHOULD be able to just switch build configs and run update-database. This has worked in every project for years, but today it just wouldn't.
When I ran a Clean, then made sure this build config was selected and the startup Project was selected, and explicitly Built, magically it was solved - even though EF appears to build everything every time you run update-database.
I have no explanation as to why this would occur, but that's the cause and solution for us.