I'm trying to setup SQL Server CE 4.0 as my Test Database in order to run integration tests, as described in this article: http://lostechies.com/erichexter/2013/01/06/using-sql-compact-for-integration-tests-with-entity-framework/
These are all the projects in my solution:
UnitTests
Website
DataAccessLayer
Core
I added a SQL Compact CE 4.0 reference and connectionString to the app.config file on the UnitTests project and then tried to run the update-database command:
update-database -ProjectName DataAccessLayer -StartupProjectName UnitTests -Verbose
This works fine for most migrations but it stops short of finishing and then I get this error:
Direct column renaming is not supported by SQL Server Compact. To
rename a column in SQL Server Compact, you will need to recreate it.
Is there a way that I can turn off migrations and just re-create the entire database JUST for the unit tests projects?
Found it. Just had to set the Database initializer on the unit tests project:
System.Data.Entity.Database.SetInitializer(new DropCreateDatabaseAlways<MyContext>());
I call this from the FixtureSetup method on my unit test project.
Related
Locally my database works fine. I try to put a server database to the same version as my development version.
In Visual Studio, I ran this command:
dotnet ef migration scripts lasknown_script_that_was_executed_on_prod_server
where I use the value of
lasknown_script_that_was_executed_on_prod_server
as found on the production server by querying:
SELECT TOP 1000
[MigrationId], [ProductVersion]
FROM
[MyProject].[dbo].[__EFMigrationsHistory]
and yes it's in my source code as well, so seems OK, my code knows that version.
After I perform that command, it creates a large output but on top of that it shows an error
Build started...
Build succeeded.
Error writing app settings | Cannot perform runtime binding on a null reference
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 6.0.5 initialized 'ApplicationDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer:6.0.5' with options: > None
BEGIN TRANSACTION;
GO
CREATE TABLE [MyApplication] ( ...my firs db create..?? ...
I assume therefore the output code is missing something that should go before the very first initial database creation.
(as my prod server is not at that version, it shouldn't start with the first migration).
Locally it works fine, however, and it was set up with code-first.
So it was generated by code, it should migrate as well by code.
Can't perform migrations on production server.
Command "dotnet ef database update" works on my computer but fails on production
Steps i tried are:
1. Fill in checkbox execute code first migrations in Visual Studio before publish.
2. dotnet ef database update not working . I installed .NET SDK but it doesn't have libraries needed.
Any suggestions appeciated.
There are a couple options:
Generate a SQL script using dotnet ef migrations script and run it on your production database.
Call dbContext.Database.Migrate() at runtime during application startup (but be careful when running multiple apps/database clients)
Also, in the next release (1.0.0-preview3) of Microsoft.EntityFrameworkCore.Tools, we'll be shipping ef.exe which you can point directly to assemblies (instead of project.json files) to perform migrations.
You can generate a migration script by running the following command:
Script-Migration -From older_migration_name -To newer_migration_name -Context ApplicationDbContext
The script will have a random name and will reside in the following path:
<Your_Project_Drive>:\<Your_Project_Folder>\<Your_Project_Folder.Model>\obj\Debug\netcoreapp3.1\<Some_Random_Name>.sql
Now just run that script on the targeted DB of production server.
I am working with Entity Framework code first.
I have managed to work with migrations, but i need to type commands in the Package Manager Console.
(for example Update-Database command).
It works fine, but it works on my developpemnt computer.
Now, imagine i have a lot of production server. Some of those servers are still in database version 1, others in version 3 and the lastest version is 5.
Is it possible to run the equivalent of Update-Database Command from C# Code ?
DbUp is one other open source tool which is effective and provides robust features to perform production DB upgrade in EF Code first approach.
Here is an informative article comparing the Dbup open source tool vs EF migration feature.
Yes,you can do that.You have to change your web.config file's connection string according to your production server and then run :
PM> Update-Database
Update : to generate scripts.
PM > Update-Database -Script -SourceMigration: $InitialDatabase -TargetMigration:
AddPostAbstract
You can refer this for more info : Getting a SQL Script
We're using EF 6, in database-first mode.
We've been building a database migration package, using FluentMigrator, and we've been running our migrations against empty LocalDb databases in our solution, for use in integration testing, etc.
Because we need our solutions to build wherever they're checked out, we've been using |DataDirectory| in the connection strings.
We've written some unit [sic] tests, running against LocalDb instances, running the migrations first to ensure that the tests are running against the current schema. (Yes, these are integration tests, but we're using a unit test framework, and I don't want to argue about it.)
What I would like to do next is to change our configuration so that when we run "Update Model from Database" on our .edmx files, it's the LocalDb instance that the tool looks to. And I can't make that work.
My guess is that whatever tool is running, when we do this, doesn't have it's DataDirectory set where I need it to be, in it's appdomain.
Does anyone know what tool this might be? And how I can configure it to connect to a localdb instance, that's using |DataDirectory| to create a relative path?
I have a test project which references another project, which in turn references a third project containing my Entity Framework 5.0 file (edmx). I find that I can run database-driven unit tests directly from Visual Studio, but when I run from mstest I get the following errors:
System.Data.Entity.Infrastructure.UnintentionalCodeFirstException:
Code generated using the T4 templates for Database First and Model
First development may not work correctly if used in Code First mode.
To continue using Database First or Model First ensure that the Entity
Framework connection string is specified in the config file of
executing application.
I have non-database connecting unit tests which run fine. I've fiddled with my various app.config EF connection strings and they seem to be OK (every tests runs fine on Visual Studio). The problem is when I run mstest from a command line on my desktop or on a build server. This is the command I use:
MSTest.exe /nologo /searchpathroot:"\Binaries"
/resultsfileroot:"\TestResults"
/testcontainer:"\Binaries\driver.orderedtest"
/testcontainer:"\Binaries\EF.DataAccess.dll"
/publish:"http://"
/publishbuild:"vstfs:///Build/Build/99" /teamproject:"ProjectName"
/platform:"Any CPU" /flavor:"Debug" /noisolation /detail:errormessage
/detail:errorstacktrace /detail:stderr
I have references to all dll's in the test project, as outlined here: Entity Framework custom tool does not correctly embed or load the model as resource files
I've been on this for a few days off and on, and it looks to me that this is an EF 5.0 bug, but according to the connect link this should be fixed, but it doesn't seem to be the case.