好的,我想在asp身份创建的用户表中添加属性。
所以我读到了它并发现我可以这样做:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public int Age { get; set; }
public IList<Blog> Blogs { get; set; }
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;
}
}
因此,属性已添加到表dbo.AspNetUsers中,而不是添加到users表中。为什么呢?
所以我想也许它只是将属性放在那里,但我可以在同一个dbset中使用它们,所以我添加了这段代码:
public class TravelContext : DbContext
{
static TravelContext()
{
System.Data.Entity.Database.SetInitializer<TravelContext>(new CreateDatabaseIfNotExists<TravelContext>());
}
public TravelContext() : base("Name=TravelDB2")
{
}
public DbSet<Blog> Blog { get; set; }
public DbSet<Continent> Continent { get; set; }
public DbSet<Country> Country { get; set; }
public DbSet<Destination> Destinations { get; set; }
public DbSet<Travel> Travel { get; set; }
public DbSet<IdentityUser> User { get; set; }
}
但这对我没有帮助。我仍然无法从我的用户那里获得firstname,我只能从Users表中访问这些属性。
那么如何让它将属性添加到正确的表中以及如何在上下文中添加它们以便我可以使用EF来使用它们? 编辑:
modelBuilder.Entity<IdentityUser>().ToTable("Users", "dbo");
modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles", "dbo");
modelBuilder.Entity<IdentityRole>().ToTable("Roles", "dbo");
modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims", "dbo");
modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogin", "dbo");
EIDT新的背景:
public class TravelContext : IdentityDbContext<ApplicationUser>
{
static TravelContext()
{
System.Data.Entity.Database.SetInitializer<TravelContext>(new CreateDatabaseIfNotExists<TravelContext>());
}
public static TravelContext Create()
{
return new TravelContext();
}
public TravelContext()
: base("Name=TravelDB2")
{
}
public DbSet<Blog> Blog { get; set; }
public DbSet<Continent> Continent { get; set; }
public DbSet<Country> Country { get; set; }
public DbSet<Destination> Destinations { get; set; }
public DbSet<Travel> Travel { get; set; }
public DbSet<IdentityUser> User { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>().ToTable("Users", "dbo");
modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles", "dbo");
modelBuilder.Entity<IdentityRole>().ToTable("Roles", "dbo");
modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims", "dbo");
modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogin", "dbo");
modelBuilder.Entity<Blog>().HasMany(a => a.Users);
modelBuilder.Entity<ApplicationUser>().HasMany(a => a.Blogs);
}
}
所以现在它将博客的id添加到我的用户表中,并将用户的id添加到我的博客实体中,但我添加了HasMany,因此它应该创建多对多关系?