ef-migrations

Entity Framework 6: How to override SQL generator?

二次信任 提交于 2019-11-29 12:33:16
问题 I'd like to amend the SQL that's being generated by EF:CF when generating the database schema (DDL), as suggested by the Entity Framework team. How can this be done? I couldn't find anything appropriate via Google. 回答1: You can override the MigrationSqlGenerator that is used by Entity Framework by calling the DbMigrationsConfiguration.SetSqlGenerator() method in the constructor of your DbMigrationsConfiguration class, passing the database provider name (e.g. "System.Data.SqlClient" for SQL

Programmatic data transformation in EF5 Code First migration

萝らか妹 提交于 2019-11-29 11:52:58
Is it possible to do any type of programmatic data transformation in Entity Framework 5 Code First migrations? There is an Sql () method to execute queries, but it has return type void and I don't see any way to get the results of the queries I perform. Example I have table Recipe with one-to-many relationship to Ingredient . For various reasons I want to convert this to a Ingredients JSON string property instead. The only approach I can think of is something like this: Create new column IngredientsJson For each recipe, query its ingredients, construct a JSON string programmatically and insert

Can one set a breakpoint in EF code first migrations seed method?

痴心易碎 提交于 2019-11-29 11:48:42
问题 I am having trouble with something in the Seed method in the Configure.cs for my entity framework 6 code-first migration process. I am running the Update-Database -verbose command in the Package Manager Console , and tried to set breakpoints (in VS studio web express 2013) in the c# code of the Seed method. But even if I put it on the first statement in the method, it is not hit, although the console displays running seed method (and subsequently breaks due to my error) So can one somehow set

How to programatically create Sql Azure database of type Basic/Standard edition through Enity Framework code first

早过忘川 提交于 2019-11-29 11:45:54
I use EF 6. My existing code is : public void CreateOrUpdateCompanyDb(string companyDbName) { try { string connectionString = _connectionStringProvider.GetConnectionString(companyDbName); DbMigrationsConfiguration cfg = CreateMigrationsConfig(connectionString); cfg.AutomaticMigrationsEnabled = false; cfg.AutomaticMigrationDataLossAllowed = false; DbMigrator dbMigrator = new DbMigrator(cfg); dbMigrator.Update(); } catch (MigrationsException exception) { _logger.Error(string.Format("Error creating company database '{0}'",companyDbName), exception); } } with connection string as follows : Server

How do I add an additional column to the __MigrationHistory table?

不问归期 提交于 2019-11-29 11:41:51
Per Customizing the Migrations History Table , I should be able to add a column, however I'm not able to find any examples on how to actually add the new column. I'm mostly confused about where to put the actual property and how to configure it to the existing __MigrationHistory table. From the documentation, I can customize the table configuration like so.. protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<HistoryRow>().ToTable(tableName: "MigrationHistory", schemaName: "admin"); modelBuilder.Entity<HistoryRow>()

Entity Framework, Code First and One-to-Many relationship across multiple contexts

最后都变了- 提交于 2019-11-29 11:41:07
I am using VS 2010 and Entity Framework code first (version 6). I have two entities each in its own context and I want to create a one-to-many relationship between them. Context 1 has the following entity: public class MyTrust { public int MyTrustID { get; set; } public string MyTrustName { get; set; } } and Context 2 has the following entity: public class MyLocation { public int MyLocationID { get; set; } public int MyTrustID { get; set; } public virtual MyTrust MyTrust { get; set; } } with the following Fluent API modelBuilder.Entity<MyLocation>() .HasRequired(m => m.MyTrust); The migration

EF5 Code First Migrations: “Column names in each table must be unique” error after using RenameColumn

本秂侑毒 提交于 2019-11-29 11:31:55
问题 We're using Entity Framework 5.0 Code First and Automatic Migrations. I had a class like so: public class TraversalZones { public int Low { get; set; } public int High { get; set; } }​ Then we realized these properties weren't really the right names, so we changed them: public class TraversalZones { public int Left { get; set; } public int Top { get; set; } }​ The rename refactored properly throughout the project, but I know Automatic Migrations aren't smart enough to pick up these explicit

How can I stop Add-Migration checking my database has no pending migrations when using Code-Based migrations?

蓝咒 提交于 2019-11-29 10:26:48
I'm investigating using Code-Based EF Migrations for a product that does not use EF. Everything generally works well, except that the command: Add-Migration MyTestMigration outputs the following message: Unable to generate an explicit migration because the following explicit migrations are pending: [201206260845338_DannyTest]. Apply the pending explicit migrations before attempting to generate a new explicit migration. The reason for this is that the connection string is not known at build time, and EF has randomly created a database called "MyContextName" on .\SQLExpress. I cannot apply the

Many-to-many relationship between entities of same type in MVC3

£可爱£侵袭症+ 提交于 2019-11-29 10:25:01
问题 I have an ASP.NET MVC3 application, where I use Entity Framework 4.3 Code-First and Migrations. I've been trying to create a many-to-many relationship between entities of the same type, but when I scaffold the migration with Migrations, it generates a one-to-one relationship. The idea is that one user should be able to follow multiple other users (think Twitter). My User model look something like this: public class User { public int UserId { get; set; } public string Name { get; set; } public

Entity Framework Code First DateTime field update on modification

牧云@^-^@ 提交于 2019-11-29 09:34:51
问题 I have the following entity: public class User { public int UserId { get; set; } public string UserName { get; set; } public string UserAddress { get; set; } public DateTime CreateDate { get; set; } public DateTime? UpdateDate { get; set; } } How can I make the UpdateDate field of the User entity become the server DateTime on an update? That is to say, whenever an entity in the database becomes modified, the UpdateDate field changes to that exact date and time as well as if to say UpdateDate