ef-migrations

How to run Seed() method of Configuration class of migrations

痴心易碎 提交于 2019-11-28 08:01:01
I have 2 questions: 1) How can I run Seed() method from the package-manager console without updating-database model? 2) Is there a way how to call Seed() method in the code? Thx for any advice. Answering your first question. Create a Migration by running add-migration SeedOnly Clear out all Up() and Down() code generated if there was any pending changes public partial class SeedOnly : DbMigration { public override void Up() { } public override void Down() { } } Then you can Target a Specific Migration by running update-database -TargetMigration SeedOnly in the Package Manager console After

How to ignore a table/class in EF 4.3 migrations

跟風遠走 提交于 2019-11-28 07:04:15
I'm testing with EF 4.3 (beta) I have some new classes which should generate db tables and columns. From a old project i have some old tables in my schema, which i want to access via EF. All Classes are declared. For accessing the old table, there is a poco which is mapped. The db migrations tries to create that old table, too. How can it set that this class/table is not part of the migration, but part of the ef model? xxx.OnModelCreating() { modelBuilder.Ignore<myOldTableClass>(); } removes the entire class from model. finally i can't use it for access via dbContext. i like to use automatic

stuck in EF migration limbo

丶灬走出姿态 提交于 2019-11-28 06:58:05
问题 i have somehow gotten my EF5 project into a state where I can't proceed. When I do an 'update-database' i get: Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration. You can use the Add-Migration command to write the pending model

How to seed data with many-to-may relations in Entity Framework Migrations

泪湿孤枕 提交于 2019-11-28 05:58:10
I use entity framework migration (in Automatic migration mode). Everything is Ok, but I have one question: How should I seed data when I have many to many relation. For example I have two model classes: public class Parcel { public int Id { get; set; } public string Description { get; set; } public double Weight { get; set; } public virtual ICollection<BuyingItem> Items { get; set; } } public class BuyingItem { public int Id { get; set; } public decimal Price { get; set; } public virtual ICollection<Parcel> Parcels { get; set; } } I understand how to seed simple data (for PaymentSystem class)

Programmatic data transformation in EF5 Code First migration

陌路散爱 提交于 2019-11-28 05:02:56
问题 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

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

不羁岁月 提交于 2019-11-28 04:50:36
问题 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

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

◇◆丶佛笑我妖孽 提交于 2019-11-28 04:49:12
问题 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

Unable to generate an explicit migration in entity framework

怎甘沉沦 提交于 2019-11-28 04:15:38
I am adding a new migration but this message shows: Unable to generate an explicit migration because the following explicit migrations are pending: [201203170856167_left]. Apply the pending explicit migrations before attempting to generate a new explicit migration. Can any one help me? It tells you that there is some unprocessed migration in your application and it requires running Update-Database before you can add another migration. I had the same problem. Apparently entity framework generates this error when it's unable to connect to the database. So make sure that you're able to access it

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

放肆的年华 提交于 2019-11-28 04:03:40
问题 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

Is it OK to update a production database with EF migrations?

痴心易碎 提交于 2019-11-28 03:08:05
According to this blog post most companies using EF Migrations are supposedly not updating the database schema of production databases with EF migrations. Instead the blog post's author recommends to use Schema update scripts as part of the deployment process. I've used Schema update scripts for a few years now and while they work, I was planning to use EF migrations instead in the future for the following reasons: Faster deployment, less downtime A simpler deployment procedure Much easier migration of existing data than it would be possible with T-SQL A more comprehensible syntax of the