我几天前开始做一个新项目,并希望在我的项目中添加一个静态表来做到这一点我尝试使用Asp.net mvc上的种子,我已经为我添加了一个静态表已经存在的角色表,但是当我尝试为我创建的表种子时,它无法找到我的类的属性,在此示例中,描述会显示下划线,因为它无法找到它。
internal sealed class Configuration : DbMigrationsConfiguration<CrowdTouring2.Models.ApplicationDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
ContextKey = "CrowdTouring2.Models.ApplicationDbContext";
}
// Cria as tabelas estaticas no sistema
protected override void Seed(CrowdTouring2.Models.ApplicationDbContext context)
{
if(!context.Roles.Any(r => r.Name == "Admin"))
{
var store = new RoleStore<IdentityRole>(context);
var manager = new RoleManager<IdentityRole>(store);
var role = new IdentityRole { Name = "Admin" };
manager.Create(role);
}
if (!context.Roles.Any(r => r.Name == "Cliente"))
{
var store = new RoleStore<IdentityRole>(context);
var manager = new RoleManager<IdentityRole>(store);
var role = new IdentityRole { Name = "Cliente" };
manager.Create(role);
}
if (!context.Roles.Any(r => r.Name == "Resolvedor"))
{
var store = new RoleStore<IdentityRole>(context);
var manager = new RoleManager<IdentityRole>(store);
var role = new IdentityRole { Name = "Resolvedor" };
manager.Create(role);
}
if (!context.Roles.Any(r => r.Name == "Avaliador"))
{
var store = new RoleStore<IdentityRole>(context);
var manager = new RoleManager<IdentityRole>(store);
var role = new IdentityRole { Name = "Avaliador" };
manager.Create(role);
}
var conta = new List<EstadoConta>
{
new EstadoConta { descricao = "Admin"}
};
}
}
我在身份模型上有我的dbSet,如下所示:
using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
namespace CrowdTouring2.Models
{
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public DbSet<EstadoConta> EstadosConta { get; set; }
}
}
我的班级
namespace CrowdTouring2.Models
{
public class EstadoConta
{
public int EstadoContaId { get; set; }
public string descricao { get; set; }
}
}
答案 0 :(得分:0)
var conta = new List<EstadoConta>
{
new EstadoConta { descricao = "Admin"}
};
conta.ForEach(s => context.contas.AddOrUpdate(p => p.descricao, s));
context.SaveChanges();