无法在ASP.NET标识中自定义配置文件

时间:2014-09-28 03:33:47

标签: entity-framework ef-code-first asp.net-identity ef-migrations

我正在尝试通过遵循此MSDN页面上的示例和来自

的Identity 2.0示例项目,对ASP.NET身份中的信息进行小规模自定义
PM> Install-Package Microsoft.AspNet.Identity.Samples -Pre

我试图添加一个名为' Region'的简单自定义字段,而不是添加一个简单的DateTime BirthDate字段。 (从一组销售区域中挑选)

我在IdentityModels.cs中创建了一个名为Region的新类

public class Region
{
    [Key]
    public Guid ID { get; set; }
    public string Name { get; set; }

    public Region()
    {
        this.ID = Guid.NewGuid();
    }
}

然后我将该类作为属性添加到ApplicationUser类中,而不是示例中的BirthDate属性。像这样:

public class ApplicationUser : IdentityUser
    {
        public Region Region { get; set; }

然后我重建并且没有错误,并运行add-migration和update-database,如下所示:

PM> add-migration "Region"
Scaffolding migration 'Region'.
The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration Region' again.
PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201409280314023_Region].
Applying explicit migration: 201409280314023_Region.
Running Seed method.

创建数据库并且Region表位于那里,但是;当我尝试输入&context ;.Regions.AddOrUpdate'种子方法,'地区'不是上下文允许的成员。 (我想用表格列表预加载表格)

protected override void Seed(SentinelPortal.Models.ApplicationDbContext context)
        {

            context.Roles.AddOrUpdate(   //this is OK

                r => r.Name,
                new IdentityRole() { Name = "Admin"},
                new IdentityRole() { Name = "RegionAdmin"},
                new IdentityRole() { Name = "User"}
            );

            context.Regions.AddOrUpdate(   //This indicates that Regions doesn't exist in 'context'
               r => r.Name,
               new Region() { Name = "NW" },
               new Region() { Name = "SW" }
            );
        }

所以我的问题是:为什么地区不属于'环境'甚至是在db?中创建表?

1 个答案:

答案 0 :(得分:0)

解决:

答案结果是我需要将它添加到ApplicationDbContect类

 public System.Data.Entity.DbSet<Region> Regions { get; set; }