MVC5 引用 ApplicationDbContext:IdentityDbContext<ApplicationUser>
MVC5 Referencing ApplicationDbContext : IdentityDbContext<ApplicationUser>
我正在使用 EF 应用程序编写 MVC4,现在正在使用 EF 将其升级到 MVC5。在 MVC4 中,我遵循了创建 DBContext 的教程,如下所示 (IssueContext.cs)。我知道在具有身份的 MVC5 中,我们的 DBContext 必须派生自 IdentityDbContext。我在我的 Seed 方法中引用 IdentityDbContext 时遇到问题。
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
},
};
The type or namespace name 'DAL' does not exist in the namespace
'RecreationalServicesTicketingSystem' (are you missing an assembly
refernce?)
DAL\IssueContext.cs(来自 MVC4 的旧数据 Class 上下文)
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>();
}
}
}
Models\IdentityModels.cs(新 Class 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();
}
}
}
您仍在引用旧的 namespace
和 class。您需要将其更改为新的上下文。由于您已经包含 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
}
我正在使用 EF 应用程序编写 MVC4,现在正在使用 EF 将其升级到 MVC5。在 MVC4 中,我遵循了创建 DBContext 的教程,如下所示 (IssueContext.cs)。我知道在具有身份的 MVC5 中,我们的 DBContext 必须派生自 IdentityDbContext。我在我的 Seed 方法中引用 IdentityDbContext 时遇到问题。
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
},
};
The type or namespace name 'DAL' does not exist in the namespace 'RecreationalServicesTicketingSystem' (are you missing an assembly refernce?)
DAL\IssueContext.cs(来自 MVC4 的旧数据 Class 上下文)
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>();
}
}
}
Models\IdentityModels.cs(新 Class 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();
}
}
}
您仍在引用旧的 namespace
和 class。您需要将其更改为新的上下文。由于您已经包含 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
}