覆盖OnModelCreating Asp.net时的EF问题

时间:2014-11-22 11:37:57

标签: asp.net-mvc entity-framework asp.net-mvc-5

我是asp.net的新手,我正在关注此博客,以扩展和修改asp.net mvc EF6.0身份版本2中的角色。

http://www.codeproject.com/Articles/727054/ASP-NET-MVC-Identity-Extending-and-Modifying-R

这是问题

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        if (modelBuilder == null)
        {
            throw new ArgumentNullException("modelBuilder");
        }

        // Keep this:
        modelBuilder.Entity<IdentityUser>().ToTable("AspNetUsers");

        // Change TUser to ApplicationUser everywhere else - 
        // IdentityUser and ApplicationUser essentially 'share' the AspNetUsers Table in the database:
        EntityTypeConfiguration<ApplicationUser> table =
            modelBuilder.Entity<ApplicationUser>().ToTable("AspNetUsers");

        table.Property((ApplicationUser u) => u.UserName).IsRequired();

        // EF won't let us swap out IdentityUserRole for ApplicationUserRole here:
        modelBuilder.Entity<ApplicationUser>().HasMany<IdentityUserRole>((ApplicationUser u) => u.Roles);
        modelBuilder.Entity<IdentityUserRole>().HasKey((IdentityUserRole r) =>
            new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("AspNetUserRoles");

        //Leave this alone:
        EntityTypeConfiguration<IdentityUserLogin> entityTypeConfiguration =
            modelBuilder.Entity<IdentityUserLogin>().HasKey((IdentityUserLogin l) =>
                new
                {
                    UserId = l.UserId,
                    LoginProvider = l.LoginProvider,
                    ProviderKey
                        = l.ProviderKey
                }).ToTable("AspNetUserLogins");

        entityTypeConfiguration.HasRequired<IdentityUser>((IdentityUserLogin u) => u.);

        EntityTypeConfiguration<IdentityUserClaim> table1 =
            modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims");

        //table1.HasRequired<IdentityUser>((IdentityUserClaim u) => u.User);

        // Add this, so that IdentityRole can share a table with ApplicationRole:
        modelBuilder.Entity<IdentityRole>().ToTable("AspNetRoles");

        // Change these from IdentityRole to ApplicationRole:
        EntityTypeConfiguration<ApplicationRole> entityTypeConfiguration1 =
            modelBuilder.Entity<ApplicationRole>().ToTable("AspNetRoles");

        entityTypeConfiguration1.Property((ApplicationRole r) => r.Name).IsRequired();
    }

问题在于这行代码

entityTypeConfiguration.HasRequired<IdentityUser>((IdentityUserLogin u) => u.User);

错误输出:

  

错误1&#39; Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin&#39;   不包含&#39;用户&#39;的定义没有扩展方法   &#39;用户&#39;接受第一个类型的参数   &#39; Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin&#39;可能   发现(您是否缺少using指令或程序集引用?)

请帮助我,非常感谢。

0 个答案:

没有答案
相关问题