【问题标题】:How to query simplemembership database如何查询simplemembership数据库
【发布时间】:2013-05-27 22:06:44
【问题描述】:

MVC4 首先使用 db 和 simplemembership

我的会员资格已经到了可以保存新用户的程度。注册此人后,我需要渲染一个新视图,以便他们填写更多信息,因此我需要将 UserProfile 表中新创建的用户 ID 传递给新视图。我尝试将模板化的注册方法修改为:

            try
            {
                WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { Email = model.Email }, true);
                WebSecurity.Login(model.UserName, model.Password);

//Get the userid for this new user
                var db = new UsersContext();
                int id = db.UserProfiles.Where(x => x.UserName == model.UserName).Select(x => x.UserId).FirstOrDefault();

                return RedirectToAction("Edit", "Profile", id);
            }

这给了我这个错误:

\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'UserProfile' 没有定义键。定义此 EntityType 的键。 \tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'UserProfiles' 基于没有定义键的类型'UserProfile'。

我认为此错误与尝试在不在 edmx 模型中的表上使用 EF 有关。我没有将任何简单成员表(UserProfile、Membership、Roles 等)添加到我的 edmx 文件中,因为这样做时会出现重复类的错误,我猜是因为 SM 表是首先创建代码的。

我从来没有先写过代码,答案就在那里吗?或者我是否需要将 SM 表添加到我的 edmx 模型中以便查询它们。如果是这样,我该如何解决重复类错误?

我的网络配置中有 2 个单独的连接字符串。

编辑++++++++++

我一直在研究并找到了不同的方法,但我得到了同样的错误。

                int id;
                using (var db = new UsersContext())
                {
                    var user = db.UserProfiles.Find(model.UserName);
                    id = user.UserId;
                }

我在“使用”行收到错误。这是 UsersContext 的代码:

public class UsersContext : DbContext
{
    public UsersContext()
        : base("SecurityEntities")
    {
    }

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

"SecurityEntities 是连接字符串,如下所示:

   <add name="SecurityEntities" connectionString="data source=(localdb)\v11.0;initial catalog=OurAgreements;user id=oaadmin;password=136VDSyPLrcfgPfw3BIH;multipleactiveresultsets=True;application name=EntityFramework" providerName="System.Data.SqlClient" />

你能看出我哪里出错了吗?

【问题讨论】:

    标签: asp.net-mvc-4 simplemembership


    【解决方案1】:

    您对 UserProfile 的定义是什么样的。它应该看起来像这样:

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

    Key属性表示UserId是key,DatabaseGeneratedAttribute表示UserId值是由数据库生成。您收到的错误似乎表明您的 UserProfile 定义中没有 Key 属性。

    2013 年 5 月 29 日更新

    如果您在更改 UserProfile 的架构时遇到问题并且未设置迁移,请查看显示 how to customize and seed SimpleMembership 的这篇文章。本文中的设置更适合开发和测试您的应用程序。 setting up database migration for SimpleMembership上还有一篇文章。

    【讨论】:

    • 感谢凯文的回复。我添加了 key 和 identity 属性,这使得执行查询成为可能,但它返回 null。然后我添加了“表”属性,它错误地指出数据库已更改,我应该使用迁移。我不能摆脱 UserProfile 模型,并将表添加到我的 edmx 文件并用作普通的 ef 连接吗?我从来没有先写过代码。
    【解决方案2】:

    我试过了

    WebSecurity.CreateUserAndAccount(model.UserName, model.Password, null, false);
    var ID = WebSecurity.GetUserId(model.UserName);
    

    对我来说效果很好

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-17
      • 2014-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多