EF migrations - call custom sql that is included into separate folder - entity-framework

For now we have used only initial migration but now our team is planning to use EF update migrations.
My question is about custom SQL:
I see that I can add custom SQL into migration
I see I can add it using SQL ("Update bla bla")
Instead I would like to add all custom SQL into a separate folder. How to call custom SQL that is included in separate folders?
P.S. We use EF 6

This feature will be available in version 6.1.2, currently available as beta:
SqlFile and SqlResource methods on DbMigration allow you to run a SQL script stored as a file or embedded resource.
Currently you can use:
var sqlFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
#"Migrations\Custom.sql");
Sql(File.ReadAllText(sqlFile));
http://blogs.msdn.com/b/adonet/archive/2014/09/18/ef6-1-2-beta-1-available.aspx
https://entityframework.codeplex.com/workitem/561

Related

Create Ef Core Migration-Script manually

Due to my current project structure where I need to handle many database-views I would like to write the Ef-Core Migration-Classes manually.
We are using the code-first-approach.
I know frameworks like the php Phinx where I can simply create migration files manually.
Is this also possible with ef core, without using database ef migrations add?
Update
I tried to create the migration file manually, but then it isn't executed on context.Database.Migrate().
Is that because of the missing .Designer-file or the Snapshot-File that is not changed?
Do I really need the Snapshot and Designer files? I guess the migration-system tracks executed migrations within the history database table?
Update 2
I changed from EF-Migrations to FluentMigrator. This module solves all my problems.

Entity Framework Core 2.0 Dynamically Creating A Database

I am new to EF Core, and as I tried using it I found out that you need to add migrations for it to create a database from models. My question is, do we have another option aside from migrations to dynamically create the database on run time just like what it was in EF 6?
Thanks.
Until a seeding mechanism is provided in EF Core you can provide your own seeding mechanism.
In earlier phases of a project, when the database is not yet fixed, I don't care that data get lost. When I want to recreate the database dynamically I call the function below. By the setting of a parameter I determine if this "recreateDatabase"-function is called yes or no. The function is included in MyOwnDbContext class.
The seed function you need to write is very similar to the one you use in EF 6.
private static void recreateDatabase(YourOwnDbContext dbContext)
{
dbContext.Database.EnsureDeleted();
dbContext.Database.EnsureCreated();
seed(dbContext);
}

Generating a database script from entity framework code-first model

I'm using entity framework with code-first (primarily because with RIA Services, you need to have some form of the code classes anyway for additional attributes).
I want to get a database generation script to easily update the visual studio database project. I don't want to use ef migrations, I prefer the database projects.
I can use something like
string sqlscript = (context as IObjectContextAdapter).ObjectContext.CreateDatabaseScript();
at runtime, but I want to have a convenient method to get the script without running the project in some way, and without using migrations.
Does anybody have any ideas?
I'm using ef4.1 and ef5 in different projects.
[EDIT: One way of answering this question would be a way to call above line from the package manager console.]
One way is using the Entity Framework Power Tools. They add a context menu entry for the file containing the DbContext class which generates a DDL file for the database.
Another way is using LinqPad. After opening the dll in LinqPad, one can execute the code snippet given in the question in a query:
(this as IObjectContextAdapter).ObjectContext.CreateDatabaseScript()
Both ways are slightly inconvenient though as they require third-party tools.
I have enabled migrations but I always do get the script at runtime without using the Package Manager Console to update the database as I have dynamic entities that can only be discovered at runtime depending on what references are included in the project.
The code to get the script looks like this:
var config = new DbMigrationsConfiguration<MyContext> { AutomaticMigrationsEnabled = true };
var migrator = new DbMigrator(config);
var scriptor = new MigratorScriptingDecorator(migrator);
string script = scriptor.ScriptUpdate(sourceMigration: null, targetMigration: null);
You can also generate custom queries using the `ObjectQuery class.
Here's an example:
var query = from emp in context.Employees
select emp;
var sqlScript = ((ObjectQuery)query).ToTraceString();
An approach you can consider is to use ef migrations to generate the script, then use MSBuild post build task to copy the script over to the database project at build time.

Where the CREATE DATABASE statement is generated in Entity Framework Code First with Migrations?

I need to configure the database created by Entity Framework Code First MigrateDatabaseToLatestVersion class.
Is it possible to influence the database files parameters like size or maxsize? What interests me in particular, is there a way to add database files?
I assume I need to find the moment where the Entity Framework generates the CREATE DATABASE statement and influence it a bit. As far as I understand, the SqlServerMigrationSqlGenerator class is too late because the database already exists at this point.
Edit: According to comment this doesn't work:
You can just add code based migration using Add-Migration command and modify its Up method to execute Sql("ALTER DATABASE ...") and do what ever you want with your database.
Database is created through DbProviderServices - that is a bridging component between EF and specific database server. ObjectContext exposes operations for creating database and generating database creation script. DbMigrator use database creation operation when you execute Update. It checks that if Database exists and if not it uses ObjectContext.CreateDatabase. So if you want to change the process of creating the database you need to implement your own implementation of the migrator to change the way how Update method generates the database (maybe you just need a decorator which will create a database prior to executing DbMigrator.Update operation).

Regenerating entity objects via EDMGEN in an MSBUILD project

I have my build executing edmgen successfully however it's generating entity objects for all of my database tables. What I'd like to have it do is only generate for select tables like it currently does in the project locally.
I'm using full generation and passing in a connection string.
There is no possibility to work with a subset of objects in EDMGen. You can try creating a new schema containing only the necessary tables, and run EDMGen for this schema only.
Alternatively, you can create your own version of EdmGen by using the EdmGen2 code base.
In the ModelGen method you can define EntityStoreSchemaFilterEntry filters to select which tables you want.