Jon*_*Jon 64 .net c# entity-framework asp.net-mvc-4 ef-migrations
我有一个项目,我想update-database
反对我,但我在一个单独的项目中有我的模型和上下文.
如果我运行,enable-migrations
我会收到此错误:在程序集"MyProject"中找不到上下文类型.
这可能是因为我的Context在MyProject.MVC中.
如果我enable-migrations
针对MyProject.MVC 运行,我必须添加一个app配置文件.我不想这样做,因为我想在许多项目中使用代码.
那么我可以enable-migrations
针对MyProject 运行并以某种方式告诉它在MyProject.MVC中查找Context吗?
SOf*_*tic 101
这仅适用于EF 6,但有一个版本将-ContextProjectName
参数添加到-enable-migrations
命令中.通过使用此命令,您可以执行以下操作:
enable-migrations -ContextProjectName MyProject.MVC -StartUpProjectName MyProject.MVC
-ContextTypeName MyProject.MVC.MyContextFolder.MyContextName -ProjectName MyProject
Run Code Online (Sandbox Code Playgroud)
这将MyProject
使用中的上下文向项目添加迁移MyProject.MVC
.您需要确保具有迁移的项目具有对您的Context的项目的引用,即MyProject
引用MyProject.MVC
小智 12
您只能在包含Database Context类的项目中运行"Enable-Migrations".
您的解决方案将包含2个项目:
1) MyProject.Models
|- Migrations
|- 201401061557314_InitialCreate.cs
|- Configuration.cs
|- MyContext.cs
|- App.config (no connection string)
Run Code Online (Sandbox Code Playgroud)
App.config中
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
Run Code Online (Sandbox Code Playgroud)
2) MyProject.MVC
|- Filters
|- InitializeSimpleMembershipAttribute.cs
Run Code Online (Sandbox Code Playgroud)
InitializeSimpleMembershipAttribute.cs
namespace MyProject.MVC.Filters
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class InitializeSimpleMembershipAttribute : ActionFilterAttribute
{
private static SimpleMembershipInitializer _initializer;
private static object _initializerLock = new object();
private static bool _isInitialized;
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// Ensure ASP.NET Simple Membership is initialized only once per app start
LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
}
private class SimpleMembershipInitializer
{
public SimpleMembershipInitializer()
{
try
{
Database.SetInitializer<MyContext>(new MigrateDatabaseToLatestVersion<MyContext, MyProject.Model.Migrations.Configuration>());
using (var context = new MyContext())
{
context.Database.Initialize(force: true);
if (!context.Database.Exists())
{
// Create the SimpleMembership database without Entity Framework migration schema
((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
}
}
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
}
catch (Exception ex)
{
throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
将MyProject.MVC设置为启动项目
在包管理器中,选择项目:MyProject.Models
然后运行"启用 - 迁移"以在MyProject.Models中创建"迁移"文件夹
接下来是"更新 - 数据库" - >迁移将使用启动项目中的Web.config中的连接字符串来执行迁移
归档时间: |
|
查看次数: |
52032 次 |
最近记录: |