2016-03-27 84 views
0

我正在使用EF應用程序編碼MVC4,現在正在使用EF將其升級到MVC5。在MVC4中,我遵循一個創建DBContext的教程(IssueContext.cs)。我知道在具有Identity的MVC5中,我們的DBContext必須從IdentityDbContext派生。我在我的Seed方法中引用IdentityDbContext時遇到了問題。MVC5引用ApplicationDbContext:IdentityDbContext <ApplicationUser>

using System; 
using System.Data.Entity; 
using System.Data.Entity.Migrations; 
using System.Linq; 
using RecreationalServicesTicketingSystem.Models; 
using System.Collections.Generic; 

internal sealed class Configuration : DbMigrationsConfiguration<RecreationalServicesTicketingSystem.DAL.IssueContext> <-- References the old one 
{ 
    public Configuration() 
    { 
     AutomaticMigrationsEnabled = false; 
    } 

    protected override void Seed(RecreationalServicesTicketingSystem.DAL.IssueContext context)<--References the old one 
    { 



//Errors on users (new users { FirstMidName.....}) 
     var users = new List<ApplicationUser> 
    { 
     new users { FirstMidName = "Jason", LastName = "Wan", 
      EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1, DepotID = 1,IsAdministrator = true}, 
     new users { FirstMidName = "Andy", LastName = "Domagas", 
      EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1,DepotID = 1,IsAdministrator = true}, 
     new users { FirstMidName = "Denis", LastName = "Djohar", 
      EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1 ,DepotID = 1,IsAdministrator = true }, 
     new users { FirstMidName = "Christine", LastName = "West", 
      EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 2, DepotID = 3,IsAdministrator = false}, 

    }; 

//Errors on UserID (... = users.Single(s => s.LastName == "Wan").UserID <--HERE) 
     var tickets = new List<Ticket> 
     { 
      new Ticket { 
       UserID = users.Single(s => s.LastName == "Wan").UserID, 
       CategoryID = categories.Single(c => c.CategoryName == "Con-X").CategoryID, 
       Issue = ("Con-X Login Error"), 
       Priority = Priority.High 
      }, 
      new Ticket { 
       UserID = users.Single(s => s.LastName == "Wan").UserID, 
       CategoryID = categories.Single(c => c.CategoryName == "Desktop").CategoryID, 
       Issue = ("Can't remote access C0123"), 
       Priority = Priority.Med 
      }, 
     }; 

類型或命名空間名稱 'DAL' 不存在命名空間 'RecreationalServicesTicketingSystem' 存在(是否缺少程序集 refernce?)

DAL \ IssueContext.cs(舊從MVC4數據類上下文)

namespace RecreationalServicesTicketingSystem.DAL 
{ 
    public class IssueContext : DbContext 
    { 
     public DbSet<User> Users { get; set; } 
     public DbSet<Ticket> Tickets { get; set; } 
     public DbSet<Category> Categories { get; set; } 
     public DbSet<Department> Departments { get; set; } 
     public DbSet<Depot> Depots { get; set; } 


     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 

     } 
    } 
} 

型號\ IdentityModels.cs(新類MVC5與身份

using Microsoft.AspNet.Identity; 
using Microsoft.AspNet.Identity.EntityFramework; 
using System.Data.Entity; 
using System.Security.Claims; 
using System.Threading.Tasks; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 
using System; 
using System.Collections.Generic; 

namespace RecreationalServicesTicketingSystem.Models 
    { 
     public class ApplicationUser : IdentityUser 
     { 
      public async Task<ClaimsIdentity> 
       GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) 
      { 
       var userIdentity = await manager 
        .CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
       return userIdentity; 
      } 

      public bool IsAdministrator { get; set; } 
      [StringLength(50, MinimumLength = 1)] 

      public string LastName { get; set; } 
      [StringLength(50, MinimumLength = 1, ErrorMessage = "First name cannot be longer than 50 characters.")] 

      [Column("FirstName")] 
      public string FirstMidName { get; set; } 

      public string FullName 
      { 
       get { return FirstMidName + " " + LastName; } 
      } 
      [DataType(DataType.Date)] 
      [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] 
      public DateTime EnrollmentDate { get; set; } 
      public int DepartmentID { get; set; } 
      [ForeignKey("DepartmentID")] 
      public virtual Department Department { get; set; } 
      public int DepotID { get; set; } 
      [ForeignKey("DepotID")] 
      public virtual Depot Depot { get; set; } 
      public virtual ICollection<Ticket> Tickets { get; set; } 
     } 


     public class ApplicationRole : IdentityRole 
     { 
      public ApplicationRole() : base() { } 
      public ApplicationRole(string name) : base(name) { } 
      public string Description { get; set; } 

     } 


     public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
     { 
      public ApplicationDbContext() 
       : base("DefaultConnection", throwIfV1Schema: false) 
      { 
      } 


      public DbSet<Ticket> Tickets { get; set; } 
      public DbSet<Category> Categories { get; set; } 
      public DbSet<Department> Departments { get; set; } 
      public DbSet<Depot> Depots { get; set; } 

      static ApplicationDbContext() 
      { 
       Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer()); 
      } 

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

添加使用RecreationalServicesTicketingSystem:? – Michael

回答

1

您仍然引用舊的namespace和類。您需要將其更改爲新的上下文。由於您已經包含using RecreationalServicesTicketingSystem.Models,所以您只需刪除舊的引用名稱空間即可。

using System; 
using System.Data.Entity; 
using System.Data.Entity.Migrations; 
using System.Linq; 
using RecreationalServicesTicketingSystem.Models; // this is the namespace that has the new context 
using System.Collections.Generic; 

internal sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext> //References the new one now 
{ 
    public Configuration() 
    { 
     AutomaticMigrationsEnabled = false; 
    } 

    protected override void Seed(ApplicationDbContext context)//References the new one now 
    {...} 
    //...other code removed for prebity 
} 
+0

應用您的解決方案後,我得到了2個新錯誤''ApplicationUser'不包含'UserID'的定義,並且沒有找到接受'ApplcationUser'類型的第一個參數的擴展方法'UserID'一個程序集引用?)'你可以看到我的Seed方法中標記的錯誤。我認爲Identity用戶有它自己的UserID?是否因爲Identity使用String而不是int? – TykiMikk

+0

不是因爲字符串。身份有一個'Id'而不是UserId'。請記住,您正在改變以前的版本,因此會有差異。這些錯誤在大多數情況下都是自我解釋的通過它們進行必要的修改。 – Nkosi

+0

但是,自從將其更改爲'UserID = users.Single(s => s.LastName ==「Wan」)。UserID,'UserID = users.Single(s => s.LastName ==「Wan」)。 Id'因爲IdentityModel的默認類型是字符串,這意味着我必須遵循這個網站:http://johnatten.com/2014/07/13/asp-net-identity-2-0-extending-identity-models-and-使用整數鍵而不是字符串/或者我可以施放它? '的toString()'? – TykiMikk