TypeLoadException:无法加载类型' Bring_My_Book_V2.Models.IdentityModels'来自汇编' Bring_My_Book_V2'

时间:2017-02-25 01:08:55

标签: c# asp.net entity-framework model-view-controller asp.net-identity

我收到以下异常。

  

TypeLoadException:无法加载类型   ' Bring_My_Book_V2.Models.IdentityModels'从装配   ' Bring_My_Book_V2'

我的DbContext课程位于Models文件夹中的Identitymodels.cs文件中。我的Identitymodels课在这里:

namespace Bring_My_Book_V2.Models {
    public class ApplicationUser : IdentityUser {
        public async Task<ClaimsIdentity>           GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
     CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this,      DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser> {
        public DbSet<User> UserInfo { get; set; }
        public DbSet<Department> Departments { get; set; }
        public DbSet<DepartmentGroup> DepartmentGroups { get; set; }

        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false) {

        }

        public static ApplicationDbContext Create() {
            return new ApplicationDbContext();
        }
    } 
}

我的数据库初始化程序类位于名为DepartmentInitiazer.cs的DAL文件夹中。

namespace Bring_My_Book_V2.DAL {
    public class DepartmentInitializer :      DropCreateDatabaseAlways<ApplicationDbContext> {
            protected override void Seed(ApplicationDbContext context) {

                var Departments = new List<Department> {
                    new Department{DepartmentName = "Chemistry"},
                    new Department{DepartmentName = "Geography and Environment"},   
                    new Department{DepartmentName = "Mathematics"},
                    new Department{DepartmentName = "Physics"},
                    new Department{DepartmentName = "Statistics"},
                    new Department{DepartmentName = "Biochemistry and Molecular Biology"},
                    new Department{DepartmentName = "Genetic Engineering and Biotechnology"},
                    new Department{DepartmentName = "Forestry and Environmental Science"},
                };

                Departments.ForEach(d => context.Departments.Add(d));
                context.SaveChanges();
           }
        }
    }
}

我的web.config文件的连接字符串是

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=   (LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-Bring_My_Book_V2- 20170224043731.mdf;Initial Catalog=aspnet-Bring_My_Book_V2-20170224043731;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

web.config中的EF设置为

 <entityFramework>
    <contexts>
      <context type="Bring_My_Book_V2.Models.IdentityModels.DefaultConnection,  Bring_My_Book_V2">
        <databaseInitializer type="Bring_My_Book_V2.DAL.DepartmentInitializer, Bring_My_Book_V2" />
      </context>
    </contexts>
    <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>

我需要更改为正确加载上下文?

1 个答案:

答案 0 :(得分:-1)

我得到了解决方案。在Web.config文件中。在实体框架中我必须改变