Asp.net mvc 4 如何查询simplemembership数据库

Asp.net mvc 4 如何查询simplemembership数据库,asp.net-mvc-4,simplemembership,Asp.net Mvc 4,Simplemembership,MVC4首先使用db和simplemembership 我的会员资格已达到可以保存新用户的程度。注册人员后,我需要呈现一个新视图,以便他们可以填写更多信息,因此我需要从UserProfile表中新创建的userid传递到新视图。我已尝试将模板寄存器方法修改为: try { WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { Em

MVC4首先使用db和simplemembership

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

            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有关。我没有将任何simplemembership表(UserProfile、Membership、Roles等)添加到我的edmx文件中,因为这样做时会出现重复类的错误,我猜是因为SM表是先通过代码创建的

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

我的web配置中有两个独立的连接字符串

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

我一直在研究,发现了不同的方法,但我得到了相同的错误

                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; }
}
public类用户上下文:DbContext
{
公共用户上下文()
:基本(“证券实体”)
{
}
公共数据库集用户配置文件{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" />


你能看到我的错误吗?

你对UserProfile的定义是什么样子的。它应该是这样的:

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    ...
 }
Key属性表示UserId是键,DatabaseGeneratedAttribute表示UserId值是由数据库生成的。您收到的错误似乎表明您的UserProfile定义中没有Key属性

2013年5月29日更新


如果您在更改UserProfile的架构时遇到问题,并且它不是为迁移而设置的,请参阅本文。本文中的设置更倾向于开发和测试您的应用程序。另一篇文章是关于的。

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

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    ...
 }
Key属性表示UserId是键,DatabaseGeneratedAttribute表示UserId值是由数据库生成的。您收到的错误似乎表明您的UserProfile定义中没有Key属性

2013年5月29日更新

如果您在更改UserProfile的架构时遇到问题,并且它不是为迁移而设置的,请参阅本文。本文中的设置更适合于开发和测试您的应用程序。还有另一篇文章。

我尝试过

WebSecurity.CreateUserAndAccount(model.UserName, model.Password, null, false);
var ID = WebSecurity.GetUserId(model.UserName);
而且它对我很有效

我试过了

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

它对我来说很好

谢谢Kevin的响应。我添加了key和identity属性,这使得可以执行查询,但返回空值。然后我添加了“Table”属性,它出错了,说数据库已经更改,我应该使用迁移。我能不能摆脱UserProfile模型,添加t能够访问我的edmx文件并用作普通ef连接吗?我从来没有先编写过代码。感谢Kevin的响应。我添加了key和identity属性,这使得可以执行查询,但返回空值。然后我添加了“Table”属性,它出错说数据库已更改,我应该使用迁移。无法我只是摆脱了UserProfile模型,将该表添加到我的edmx文件中,并将其用作普通ef连接?我从来没有先编写过代码。