将用户添加到数据库

时间:2017-04-18 14:00:48

标签: c# mysql asp.net-core entity-framework-core

我收到错误:

  

InvalidOperationException :实体类型' 用户'没找到。确保已将实体类型添加到模型中。

当我输入要添加到数据库的信息并提交我收到错误时,我可以让页面加载它。我通过 RegisterViewModel.cs 运行它,并带有指向 user.cs 的链接。表名是用户。希望有人能发现我确信我遗失的简单物品。

我发现很多人都有类似的错误,但不能解决我的问题。我已将User更改为User以匹配数据库以确保。我不知道它是否通过我的验证检查运行。

Homecontroller.cs

[HttpPost]
[Route("Register")]
public IActionResult Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        Users NewPerson = new Users
        {
            FirstName = model.FirstName,
            LastName = model.LastName,
            EmailAddress = model.Email,
            Password = model.Password,
            CreatedAt = DateTime.Now,
            UpdatedAt = DateTime.Now,
        };

        _context.Add(NewPerson);
        // OR _context.Users.Add(NewPerson);
        _context.SaveChanges();
    }
    return View(model);
}

RegisterViewModles.cs

using System.ComponentModel.DataAnnotations;
namespace BankAccounts.Models
{
    public class RegisterViewModel : BaseEntity
    {
        [Required]
        [MinLength(2)]
        [RegularExpression(@"^[a-zA-Z]+$")]
        public string FirstName { get; set; }

        [Required]
        [MinLength(2)]
        [RegularExpression(@"^[a-zA-Z]+$")]
        public string LastName { get; set; }


        [Required]
        [EmailAddress]
        public string Email { get; set; }

        [Required]
        [MinLength(8)]
        [DataType(DataType.Password)]
        public string Password { get; set; }

        [Compare("Password", ErrorMessage = "Password and confirmation must match.")]
        public string PasswordConfirmation { get; set; }
    }
}

User.cs

using System;
namespace BankAccounts.Models
{
    public class Users : BaseEntity
    {
        public int UserId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmailAddress { get; set; }
        public string Password {get; set;}
        public DateTime CreatedAt { get; set; }
        public DateTime UpdatedAt { get; set; }
    }
}

数据库列

UserId
FirstName
LastName
EmailAddress
Password
CreatedAt
UpdatedAt

dbconnector.cs

using System.Collections.Generic;
using System.Data;
using Microsoft.Extensions.Options;
using MySql.Data.MySqlClient;

namespace BankAccounts
{
    public class DbConnector
    {
        private readonly IOptions<MySqlOptions> MySqlConfig;

        public DbConnector(IOptions<MySqlOptions> config)
        {
            MySqlConfig = config;
        }
        internal IDbConnection Connection {
            get {
                return new MySqlConnection(MySqlConfig.Value.ConnectionString);
            }
        }

        //This method runs a query and stores the response in a list of dictionary records
        public List<Dictionary<string, object>> Query(string queryString)
        {
            using(IDbConnection dbConnection = Connection)
            {
                using(IDbCommand command = dbConnection.CreateCommand())
                {
                   command.CommandText = queryString;
                   dbConnection.Open();
                   var result = new List<Dictionary<string, object>>();
                   using(IDataReader rdr = command.ExecuteReader())
                   {
                      while(rdr.Read())
                      {
                          var dict = new Dictionary<string, object>();
                          for( int i = 0; i < rdr.FieldCount; i++ ) {
                              dict.Add(rdr.GetName(i), rdr.GetValue(i));
                          }
                          result.Add(dict);
                      }
                   }
                   return result;
                }
            }
        }
        //This method run a query and returns no values
        public void Execute(string queryString)
        {
            using (IDbConnection dbConnection = Connection)
            {
                using(IDbCommand command = dbConnection.CreateCommand())
                {
                    command.CommandText = queryString;
                    dbConnection.Open();
                    command.ExecuteNonQuery();
                }
            }
        }
    }
}

BankAccountContext.cs

using Microsoft.EntityFrameworkCore;

namespace BankAccounts.Models
{
    public class BankAccountsContext : DbContext
    {
        // base() calls the parent class' constructor passing the "options" parameter along
        public BankAccountsContext(DbContextOptions<BankAccountsContext> options) : base(options) { }
    }
}

0 个答案:

没有答案