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

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.

Related

Replace config value in teamcity build step

I have a build step in teamcity which allows me to replace web.config values with web.release.config values. I would like to add one more step - pass some parameter from teamcity to web.config. In my case it will be release version, which is part of connection string. The best idea I have is just to have some powershell script which will replace text in some file (web.config). Are there any better options?
Example web config
<add key="Version" value="Replace me, please from teamcity"/>
<add key="some key" value="example 2. version as part of some value #VERSION"/>
You could use File Content Replacer.
Instead of making the creation of the web.config in release dependent of TeamCity, you can use the web.config transformation syntax
ie: To create a specific connection string:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="MyDB"
connectionString="value for the deployed Web.config file"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
</configuration>

"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.

Specify Machine Name in Web.Config Transform

I am using Web.config transforms to successfully create debug and release versions of the my web.config - this is working correctly.
I am interested to know whether there is a 'machine name' property to specify the current machine name which I can use in a debug URL, rather than hard-coding a specific machine name (using localhost isn't an option in the case), e.g.
<add name="XrmService" connectionString="http://$(ComputerName):5555/Service.svc" />
Are there any properties available using Web.config transforms? Similar to MSBuild's $(ComputerName) property?
I faced a similar issue, what I ended up doing is :
1) Added the following build target to the project file. (Which is an MSBuild script effectively)
<Target Name="AfterBuild">
<TransformXml Source="Web.config" Condition="Exists('Web.$(Computername).config') " Transform="Web.$(Computername).config" Destination="Web.config" />
</Target>
2) Added a Web.MyMachineName.config config transform file to the project. In your case it should look like this:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="XrmService"
connectionString="http://MyMachineName:5555/Service.svc"
xdt:Transform="SetAttributes"
xdt:Locator="Match(name)"/>
</connectionStrings>
</configuration>
This has the benefit of running different transformations based on the machine name, without creating a separate build configuration. You can configure it to be debug only by specifying Condition="'$(Configuration)' == 'Debug'".
There is an Environment Variable that you can use. It is $(COMPUTERNAME).
Open a command window, type "set" (without the double quotes) and press Enter. You will see this Environment Variable somewhere at the top of the screen.

Having trouble with LinqPad Adding a Connection to Entity Framework

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>

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.