c# - 自定义 SimpleMembership

标签 c# asp.net-mvc-4 entity-framework-5 simplemembership

看完这篇不错BLOG关于向 UserProfile 表添加自定义数据,我想将其更改为 UserProfile 表存储默认数据的方式 + 另一个存储所有附加信息的类。

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

学生.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; }
}

用户配置文件.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 定义。我的数据库上下文类如下所示:

MvcLoginDb.cs

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

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

再次,我从 AccountModel.cs 中删除了数据库上下文定义。

我在 Package-Manager-Console 中写了:

Enable-Migrations

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

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" }
                });

    }
}

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

谁能解释为什么我会收到这个错误,我是否应该使用不同的方法在不同的表中存储额外的数据?

最佳答案

我在同一个问题上苦苦挣扎。关键是让 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; }
}

用户配置文件.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> 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 方法创建用户。所以我最终使用 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<string, object>{ {"PersonId" , person.Id}}
     );

 }

关于c# - 自定义 SimpleMembership,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15177556/

相关文章:

c# - 快速查找可枚举对象中的结果

asp.net - 在 ASP.NET MVC 4 中,子文件夹中的 _viewStart 文件是附加的还是单独的?

css - CSS 中的 ID 被 <a> 颜色属性覆盖

wcf-data-services - 为什么我的 WCF DataService 没有加载扩展属性?

.net - Entity Framework 代码优先 : One-to-One where PK is a FK

C# 从泛型方法调用泛型方法

c# - WCF Discovery 返回硬编码的 URL

c# - 如何获取套接字异常值

c# - 如何为验证属性错误消息组合资源字符串?

.net - 强制 EF POCO 实体相关集合上的 linq-to-entities(类似于 EntityCollection<>)