visual update tutorial studio mvc migrations framework first existing español enable code automatic asp.net-mvc entity-framework ef-code-first code-first-migrations

asp.net-mvc - update - mvc entity framework español



Múltiples contextos DB en la misma base de datos y aplicación en EF 6 y Code First Migrations (1)

Soy nuevo en Entity Framework. Estoy intentando configurar una aplicación MVC que usa EF 6. Estoy usando Code First Migrations. Estoy usando áreas en la aplicación y me gustaría tener diferentes DbContexts en cada área para dividirlo. Sé que EF 6 tiene ContextKey, pero no puedo encontrar información completa sobre cómo usarlo. Actualmente solo puedo usar migraciones en un contexto a la vez.

¿Puede alguien dar un ejemplo con detalles suficientes para que una nueva persona como EF lo entienda y lo use?


Entity Framework 6 agregó soporte para múltiples DbContext s al agregar los -ContextTypeName y -MigrationsDirectory . Acabo de ejecutar los comandos en mi consola de Package Manager y pegué el resultado a continuación ...

Si tiene 2 DbContext s en su proyecto y ejecuta enable-migrations , obtendrá un error (como probablemente ya sepa):

PM> enable-migrations More than one context type was found in the assembly ''WebApplication3''. To enable migrations for ''WebApplication3.Models.ApplicationDbContext'', use Enable-Migrations -ContextTypeName WebApplication3.Models.ApplicationDbContext. To enable migrations for ''WebApplication3.Models.AnotherDbContext'', use Enable-Migrations -ContextTypeName WebApplication3.Models.AnotherDbContext.

Por lo tanto, debe ejecutar enable-migrations en cada DbContext separado. Y debe especificar una carpeta para cada archivo Configuration.cs que se generará ...

PM> Enable-Migrations -ContextTypeName ApplicationDbContext -MigrationsDirectory Migrations/ApplicationDbContext Checking if the context targets an existing database... Code First Migrations enabled for project WebApplication3. PM> Enable-Migrations -ContextTypeName AnotherDbContext -MigrationsDirectory Migrations/AnotherDbContext Checking if the context targets an existing database... Code First Migrations enabled for project WebApplication3.

Para agregar migraciones para cada DbContext , hágalo así especificando el nombre completo de la clase de Configuration :

PM> Add-Migration -ConfigurationTypeName WebApplication3.Migrations.ApplicationDbContext.Configuration "InitialDatabaseCreation" Scaffolding migration ''InitialDatabaseCreation''. The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running ''Add-Migration InitialDatabaseCreation'' again. PM> Add-Migration -ConfigurationTypeName WebApplication3.Migrations.AnotherDbContext.Configuration "InitialDatabaseCreation" Scaffolding migration ''InitialDatabaseCreation''. The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running ''Add-Migration InitialDatabaseCreation'' again.

Y ejecuta update-database la misma manera:

PM> Update-Database -ConfigurationTypeName WebApplication3.Migrations.ApplicationDbContext.Configuration Specify the ''-Verbose'' flag to view the SQL statements being applied to the target database. Applying explicit migrations: [201402032113124_InitialDatabaseCreation]. Applying explicit migration: 201402032113124_InitialDatabaseCreation. Running Seed method. PM> Update-Database -ConfigurationTypeName WebApplication3.Migrations.AnotherDbContext.Configuration Specify the ''-Verbose'' flag to view the SQL statements being applied to the target database. Applying explicit migrations: [201402032113383_InitialDatabaseCreation]. Applying explicit migration: 201402032113383_InitialDatabaseCreation. Running Seed method.

Espero que esto ayude.