未使用映射器映射文件

问题描述 投票:0回答:1

我有一个用户模型:

namespace Pharma.Models
{
    public partial class User
    {
        public int UserId { get; set; }
        public string FirstName { get; set; }
        public string Email { get; set; }
        public bool Active { get; set; }

        public User()
        {
            if (FirstName == null)
            {
                FirstName = "";
            }

            if (Email == null)
            {
                Email = "";
            }

        }

    }

}

和dto:

namespace Pharma.DTOs
{
    public partial class UserForRegistrationDto
    {
        public string Email { get; set; }
        public string Password { get; set; }
        public string PasswordConfirm { get; set; }
        public string FirstName { get; set; }



        public UserForRegistrationDto()
        {
            if (Email == null)
            {
                Email = "";
            }
            if (Password == null)
            {
                Password = "";
            }
            if (PasswordConfirm == null)
            {
                PasswordConfirm = "";
            }
            if (FirstName == null)
            {
                FirstName = "";
            }

        }
    }
}

然后:

public class AuthController
{
    private readonly AuthHelper _authHelper;
    IAuthRepository _authRepository;
    IUserRepository _userRepository;

    IMapper _mapper;



    public AuthController(IConfiguration config, IAuthRepository authRepository, IUserRepository userRepository)
    {
        _authHelper = new AuthHelper(config);
        _authRepository = authRepository;
        _userRepository = userRepository;
        _mapper = new Mapper(new MapperConfiguration(cfg =>
       {
           cfg.CreateMap<UserForRegistrationDto, User>();
       }));

    }

    [HttpPost("Register")]
    public async void RegisterAsync(UserForRegistrationDto userForRegistration)
    {
        byte[] passwordSalt = new byte[128 / 8];
        // check if password and conformation are the same
        if (userForRegistration.Password != userForRegistration.PasswordConfirm)
        {
            throw new Exception("Incorrect credentials!");
        }
        // check if user with same email already exists
        if (await _authHelper.DoesUserExistByEmailAsync(userForRegistration.Email!))
        {
            throw new Exception("User already exists!");
        }
        
_authHelper.GetPasswordHash(userForRegistration.Password, passwordSalt);
            byte[] passwordHash = [];
            byte[] passwordHashed = _authHelper.GetPasswordHash(userForRegistration.Password, passwordSalt);
            string EmailForRegister = userForRegistration.Email;
     
        List<SqlParameter> sqlParameters = new List<SqlParameter>();
       
        SqlParameter passwordSaltParameter = new SqlParameter("@PasswordSalt", SqlDbType.VarBinary);
        passwordSaltParameter.Value = passwordSalt;
        SqlParameter passwordHashParameter = new SqlParameter("@PasswordHash", SqlDbType.VarBinary);
        passwordHashParameter.Value = passwordHash;
        sqlParameters.Add(passwordSaltParameter);
        sqlParameters.Add(passwordHashParameter);
        Console.WriteLine(sqlParameters);
       
        UserToRegisterAuthDto userToRegisterAuthDto = new UserToRegisterAuthDto
        {
            Email = EmailForRegister,
            passwordHash = passwordHashed,
            passwordSalt = passwordSalt
        };
      
        _authRepository.AddEntity<UserToRegisterAuthDto>(userToRegisterAuthDto);
    
        User userDb = _mapper.Map<User>(userForRegistration);
        _userRepository.AddEntity<User>(userDb);
        _userRepository.SaveChanges();
        _authRepository.SaveChanges();
        

当我想添加到

userDb
我的数据库时,我没有名字;正在添加电子邮件,但没有名字,就像它没有映射一样;为什么?模型和 DTO 都有一个名为 FirstName 的字段,因此 SQL 表(它有 UserId、FirstName、Email、Active)字段。我认为如果字段具有相同的名称,并且 SQL 表有一列及其名称,无需更多配置即可工作。

编辑:

using Microsoft.EntityFrameworkCore;
using Pharma.Models;

namespace Pharma.Data
{
    public class DataContextEF : DbContext
    {
        private readonly IConfiguration _config;

        public DataContextEF(IConfiguration config)
        {
            _config = config;
        }

        public virtual DbSet<User> Users { get; set; }
        public virtual DbSet<Substance> Substances { get; set; }
        public virtual DbSet<Dosage> Dosages { get; set; }
        public virtual DbSet<Product> Products { get; set; }

        public virtual DbSet<UserToRegisterAuthDto> UsersAuth { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder
                    .UseSqlServer(_config.GetConnectionString("DefaultConnection"),
                        optionsBuilder => optionsBuilder.EnableRetryOnFailure());
            }
        }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasDefaultSchema("dbo");

            modelBuilder.Entity<User>()
                .ToTable("Users", "dbo")
                .HasKey(u => u.UserId);

            modelBuilder.Entity<Substance>()
                .ToTable("Substances", "dbo")
                .HasKey(u => u.SubstanceId);

            modelBuilder.Entity<Dosage>()
                .ToTable("Dosages", "dbo")
                .HasKey(u => u.DosageId);

            modelBuilder.Entity<Product>()
                .ToTable("Products", "dbo")
                .HasKey(u => u.ProductId);

            modelBuilder.Entity<UserToRegisterAuthDto>()
                .ToTable("Auth2", "dbo")
                .HasKey(u => u.Email);

        }

    }

}

我添加了数据上下文

c# .net automapper
1个回答
0
投票
UserToRegisterAuthDto userToRegisterAuthDto = new UserToRegisterAuthDto
        {
            Email = EmailForRegister,
            passwordHash = passwordHashed,
            passwordSalt = passwordSalt
        }

您没有在源对象实例上设置名字

© www.soinside.com 2019 - 2024. All rights reserved.