程序包管理器控制台抛出错误,无法启用迁移。
错误消息是:
的类型初始值设定项 ' System.Data.Entity.Migrations.DbMigrationsConfiguration`1'扔了一个 异常。
这是我的模特[员工]:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.Web;
namespace MVCCMigration.Models
{
public enum Gender {Male,Female,Other }
public class Employee
{
[Key]
public int EmpId { get; set; }
[Display(Name="Employee Name")]
[Required]
public string EmpName { get; set; }
[Required]
public Gender Gender { get; set; }
[Required]
[Display(Name = "Department")]
public int DepartmentId { get; set; }
public virtual Department DepartmentName { get; set; }
}
public class Department
{
[Key]
public int DepartmentId { get; set; }
[Required]
[Display(Name = "Department Name")]
public string DepartmentName { get; set; }
}
}
我的模型[EmployeeContext]:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace MVCCMigration.Models
{
public class EmployeeContext : DbContext
{
public EmployeeContext() : base("name=EmployeeContext") {
}
public DbSet<Department> department { get; set; }
public DbSet<Employee> employee { get; set; }
}
}
这是我的Web.Config:
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=301880
-->
<configuration>
<connectionStrings>
<add name="EmployeeContext" connectionString=" Data Source=LAPTOP-TL0BA7EM\LOCAL; Initial Catalog= EmployeeContext2016; user id = sa; Password= aaa; Integrated Security=False" providerName ="System.Data.Client"/>
</connectionStrings>
<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>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration
&GT;
在我输入enable-migrations -contexttypename EmployeeContext
之后,它会抛出如上所述的错误消息:
的类型初始值设定项 &#39; System.Data.Entity.Migrations.DbMigrationsConfiguration`1&#39;扔了一个 异常。
请帮助。我是C#/ ASP.NET的初学者,以及Code First迁移,我正在为教育目的尝试一些东西。非常感谢你!