自定义SimpleMembership

在阅读了关于向UserProfile表添加自定义数据的好BLOG之后,我想以UserProfile表应该存储默认数据的方式更改它+另一个存储所有其他信息的类。

使用Interenet应用程序模板创建新项目后,我创建了两个类:

Student.cs

[Table("Student")] public class Student { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public virtual int StudentId { get; set; } public virtual string Name { get; set; } public virtual string Surname { get; set; } public virtual int UserId { get; set; } } 

UserProfile.cs

 [Table("UserProfile")] public class UserProfile { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int UserId { get; set; } public string UserName { get; set; } public virtual int StudentId { get; set; } public virtual Student Student { get; set; } } 

另外,我从AccountModel.cs中删除了UserProfile定义。 我的DB上下文类看起来像这样:

MvcLoginDb.cs

 public class MvcLoginDb : DbContext { public MvcLoginDb() : base("DefaultConnection") { } public DbSet UserProfiles { get; set; } public DbSet Students { get; set; } } 

我再次从AccountModel.cs中删除了db上下文定义。

内部包管理器 – 控制台我写道:

 Enable-Migrations 

我的Configuration.cs看起来像这样:

 internal sealed class Configuration : DbMigrationsConfiguration { public Configuration() { AutomaticMigrationsEnabled = true; } protected override void Seed(MvcLogin.Models.MvcLoginDb context) { WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); if (!WebSecurity.UserExists("banana")) WebSecurity.CreateUserAndAccount( "banana", "password", new { Student = new Student { Name = "Asdf", Surname = "Ggjk" } }); } } 

这是将学生数据添加为创建新类的想法 ,但这种方法不起作用,因为在运行Update-Database -Verbose之后我收到错误: 从对象类型MvcLogin.Models.Student到已知托管不存在映射提供者本机类型。

任何人都可以说明为什么我会收到此错误,我应该使用不同的方法在不同的表中存储其他数据?

我在同样的问题上苦苦挣扎。 关键是要使UserProfile类和Student类之间的关系正确。 它必须是一对一的关系才能正常工作。

这是我的解决方案:

Person.cs

 public class Person { [Key] public int Id { get; set; } public string Name { get; set; } /* and more fields here */ public virtual UserProfile UserProfile { get; set; } } 

UserProfile.cs

 public class UserProfile { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int UserId { get; set; } public string UserName { get; set; } public virtual Person Person { get; set; } } 

MyDbContext.cs

 public class MyDbContext : DbContext { public DbSet Person { get; set; } public DbSet UserProfile { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity() .HasRequired(u => u.Person) .WithOptional(p => p.UserProfile) .Map(m => m.MapKey("PersonId")); } } 

但是,我也在努力使用WebSecurity.CreateUserAndAccount方法创建用户。 所以我最终使用Entity Framework创建了Person对象,然后使用成员资格提供程序方法创建了用户帐户:

AccountRepository.cs

  public Person CreatePerson(string name, string username) { Person person = new Person { Name = name }; _dbContext.Person.add(person); _dbContext.SaveChanges(); var membership = (SimpleMembershipProvider)Membership.Provider; membership.CreateUserAndAccount( model.UserName, createRandomPassword(), new Dictionary{ {"PersonId" , person.Id}} ); } 

HenningJ,我正在寻找同样的答案。 不幸的是(我确定你知道)你这样做的问题是没有SQL事务。 您的SaveChanges()或CreateUserAndAccount()可能会失败,使您的用户数据库处于无效状态。 如果一个失败,您需要回滚。

我仍然在寻找最终的答案,但我会发布我最终的解决方案。 希望避免编写自定义成员资格提供程序(AGAIN!),但开始认为它会更快。