定制SimpleMembership [英] Customizing SimpleMembership

查看:189
本文介绍了定制SimpleMembership的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读本好后,<一个href=\"http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-$c$cfirst-and-custom-user-properties/\"相对=nofollow>博客有关添加自定义数据表的用户配置,我想改变它的用户配置表应存储的默认数据+,所有其他信息存储在另一个类的方式。

After reading this good BLOG about adding custom data to UserProfile table, I wanted to change it the way that UserProfile table should store default data + another class where all additional info is stored.

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

After creating new project using Interenet application template, I have created two classes:

Student.cs

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

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的用户配置的定义。我的数据库上下文类看起来是这样的:

also, I've deleted the UserProfile definition from AccountModel.cs. My DB context class looks like this:

MvcLoginDb.cs

MvcLoginDb.cs

public class MvcLoginDb : DbContext
{
    public MvcLoginDb()
        : base("DefaultConnection")
    {
    }

    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<Student> Students { get; set; }
}

从AccountModel.cs再次,我已经删除的数据块上下文定义。

again, I have deleted db context definition from AccountModel.cs.

内包装管理器控制台我写:

Inside Package-Manager-Console I've written:

Enable-Migrations

和我Configuration.cs是这样的:

and my Configuration.cs looks like this:

internal sealed class Configuration : DbMigrationsConfiguration<MvcLogin.Models.MvcLoginDb>
{
    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" }
                });

    }
}

这是将学生的数据作为创建一个新类的想法,但这种做法是不运行后,由于工作的更新,数据库-Verbose 的我得到的错误:
否从映射对象类型MvcLogin.Models.Student到一个已知的托管提供原生类型的存在。

That was the idea of adding student data as creating a new class, but this approach is not working because after running Update-Database -Verbose I'm getting the error: No mapping exists from object type MvcLogin.Models.Student to a known managed provider native type.

谁能expain为什么我得到这个错误,我768,16使用不同的方法在不同的表中存储更多的数据?

Can anyone expain why I'm getting this error, shoud I use a different approach for storing additional data in different table?

推荐答案

我挣扎了很多同样的问题。关键是让用户配置类和你的学生正确的类之间的关系。它必须以正确的工作,一比一的关系。

I struggled a lot with the same issue. The key is to get the relation between the UserProfile class and your Student class correct. It have to be a one-to-one relationship in order to work correct.

下面是我的解决方案:

Person.cs

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

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

MyDbContext.cs

public class MyDbContext : DbContext
{
    public DbSet<Person> Person { get; set; }
    public DbSet<UserProfile> UserProfile { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
     modelBuilder.Entity<UserProfile>()
                    .HasRequired(u => u.Person)
                    .WithOptional(p => p.UserProfile)
                    .Map(m => m.MapKey("PersonId"));
    }
}

不过,我也挣扎与WebSecurity.CreateUserAndAccount方法创建用户。所以,我结束了创建我与实体框架Person对象,然后创建成员资格提供方法的用户帐户:

However, I also struggled with creating users with the WebSecurity.CreateUserAndAccount method. So I ended up creating my Person objects with Entity Framework and then create the user account with the membership provider method:

AccountRepository.cs

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<string, object>{ {"PersonId" , person.Id}}
     );

 }

这篇关于定制SimpleMembership的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆