我有一个有三层的应用程序。我正在使用EntityFramework 6.1并使用现有数据库编写第一个迁移代码。下面给出了三个层:
1. Presententation Layer :ConceptuCodeFirstMigrationDemo 简单的控制台应用程序
数据层由Context和Initializer组成。代码如下:
namespace ConceptuCodeFirstMigrationDemo.Data.DataContext
{
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using ConceptuCodeFirstMigrationDemo.Domain;
public partial class ConceptuContext : DbContext,IDisposable
{
public ConceptuContext()
: base("name=ConceptuContext")
{
}
public virtual DbSet<User> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.WithRequired(e => e.User)
.HasForeignKey(e => e.CreatedBy)
.WillCascadeOnDelete(false);
}
}
}
ConceptuInitializer.Cs
namespace ConceptuCodeFirstMigrationDemo.Data.DataContext
{
public class ConceptuInitializer: System.Data.Entity.CreateDatabaseIfNotExists<ConceptuContext>
{
}
}
的AppConfig:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<contexts>
<context type="ConceptuCodeFirstMigrationDemo.Data.DataContext.ConceptuContext, ConceptuCodeFirstMigrationDemo.Data">
<databaseInitializer type="ConceptuCodeFirstMigrationDemo.Data.DataContext.ConceptuInitializer, ConceptuCodeFirstMigrationDemo.Data" />
</context>
</contexts>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="ConceptuContext" connectionString="data source=AMITABHA-PC;initial catalog=Conceptu;user id=sa;password=lovediya;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
域层:由实体类组成,例如用户
尝试使用Enable-Migrations启用迁移时编写新类 展示 在程序集中找不到上下文类型&#39; ConceptuCodeFirstMigrationDemo&#39;。
尝试以下命令后: 启用 - 迁移--ContextTypeName ConceptuCodeFirstMigrationDemo.Data.DataContext.ConceptuContext
这显示以下错误 上下文类型&#39; ConceptuCodeFirstMigrationDemo.Data.DataContext.ConceptuContext&#39;未在装配中找到&#39; ConceptuCodeFirstMigrationDemo&#39;。
尝试以下命令后 启用 - 迁移ConceptuCodeFirstMigrationDemo.Data.DataContext.ConceptuContext
显示以下错误
启用 - 迁移:找不到接受参数&#39; ConceptuCodeFirstMigrationDemo.Data.DataContext.Concept的位置参数 uContext&#39 ;. 在线:1字符:18 +启用 - 迁移&lt;&lt;&lt;&lt; ConceptuCodeFirstMigrationDemo.Data.DataContext.ConceptuContext + CategoryInfo:InvalidArgument:(:) [Enable-Migrations],ParameterBindingException + FullyQualifiedErrorId:PositionalParameterNotFound,启用 - 迁移
答案 0 :(得分:0)
试试这个。
Enable-Migrations -ContextTypeName ConceptuCodeFirstMigrationDemo.Data.DataContext.ConceptuContext -ProjectName ConceptuCodeFirstMigrationDemo