扩展的ASP.NET用户不保存到数据库中

时间:2021-03-20 12:41:46

I want to make a music website /with educational purposes/. I wanted to use ASP.NET Identity User, but also I wanted to add some extra properties. I am doing code first. So I made a Model layer where I am extending the Identity User like this:

我想建立一个音乐网站/有教育目的/。我想使用ASP.NET Identity User,但我还想添加一些额外的属性。我先做代码。所以我创建了一个Model层,我正在扩展Identity User,如下所示:

public class User : IdentityUser
{
    //Some extra data that I want to add goes here

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> manager, string authenticationType)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
        return userIdentity;
    }
}

So far so good. Next I implemented my Data layer with Repository and Unit Of Work patterns and on the top of them is my Services layer /MVC project/. I ran it and I registered an User and I was redirected as that user. I though everything is okay so I decided to check my database. I saw the tables, provided by ASP.NET, but they were empty. I tried to register the same user and I was noticed that there is an user with this email, already.

到现在为止还挺好。接下来,我使用Repository和Unit Of Work模式实现了我的数据层,其中最重要的是我的服务层/ MVC项目/。我跑了它,我注册了一个用户,我被重定向为该用户。我虽然一切都还好,所以我决定检查我的数据库。我看到了ASP.NET提供的表,但它们都是空的。我试图注册同一个用户,我注意到有一个用户已经收到了这封电子邮件。

I checked the connection strings and everything was fine. I am running out of ideas...

我检查了连接字符串,一切都很好。我的想法已经不多了......

扩展的ASP.NET用户不保存到数据库中

I am totally mistaken somewhere, but I can find it. I will attach my project if someone wants to see the code.

我在某处完全错了,但我能找到它。如果有人想查看代码,我会附上我的项目。

Source code

2 个解决方案

#1


0  

So what happens is that yes, it is being inserted into the database. However, the data table is not being refreshed. What I use to solve the issue is something like this:

那么会发生什么,是的,它被插入到数据库中。但是,数据表未刷新。我用来解决这个问题的是这样的:

   private void showData()
    {
        string conString = @"your;connection;string;"

        SqlDataAdapter sda;
        DataTable dt;


        SqlConnection con = new SqlConnection(conString);
        sda = new SqlDataAdapter("SELECT * FROM yourTableName", con);


        dt = new DataTable();
        sda.Fill(dt);
        dataGridView1.DataSource = dt;
    }

Then just run showData() on load or on click of a refresh button.

然后在加载或单击刷新按钮时运行showData()。

I had the same issue when designing my own application. Hope this helps.

我在设计自己的应用程序时遇到了同样的问题。希望这可以帮助。

#2


0  

Okay so I found what was wrong in the whole picture. In the models there is an ApplicationDbContext.

好的,所以我发现整个画面出了什么问题。在模型中有一个ApplicationDbContext。

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DatabaseConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

There by default the connection is set on DefaultConnection so it does not work how I wanted to.

默认情况下,在DefaultConnection上设置连接,因此它不能按我的意愿工作。

#1


0  

So what happens is that yes, it is being inserted into the database. However, the data table is not being refreshed. What I use to solve the issue is something like this:

那么会发生什么,是的,它被插入到数据库中。但是,数据表未刷新。我用来解决这个问题的是这样的:

   private void showData()
    {
        string conString = @"your;connection;string;"

        SqlDataAdapter sda;
        DataTable dt;


        SqlConnection con = new SqlConnection(conString);
        sda = new SqlDataAdapter("SELECT * FROM yourTableName", con);


        dt = new DataTable();
        sda.Fill(dt);
        dataGridView1.DataSource = dt;
    }

Then just run showData() on load or on click of a refresh button.

然后在加载或单击刷新按钮时运行showData()。

I had the same issue when designing my own application. Hope this helps.

我在设计自己的应用程序时遇到了同样的问题。希望这可以帮助。

#2


0  

Okay so I found what was wrong in the whole picture. In the models there is an ApplicationDbContext.

好的,所以我发现整个画面出了什么问题。在模型中有一个ApplicationDbContext。

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DatabaseConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

There by default the connection is set on DefaultConnection so it does not work how I wanted to.

默认情况下,在DefaultConnection上设置连接,因此它不能按我的意愿工作。