自动映射器具有未映射属性的问题



好的,首先,我要做的是从 angular 的注册表单中获取用户详细信息,并将这些详细信息注册到 SQL Server 数据库中。我一直在关注几个教程,我想知道为什么当我运行命令进行新迁移时,它会创建一个具有许多不同属性的表,我没有指定。

例如,默认情况下,它会创建以下属性:

migrationBuilder.CreateTable(
name: "User",
columns: table => new
{
Id = table.Column<string>(nullable: false),
UserName = table.Column<string>(maxLength: 256, nullable: true),
NormalizedUserName = table.Column<string>(maxLength: 256, nullable: true),
Email = table.Column<string>(maxLength: 256, nullable: true),
NormalizedEmail = table.Column<string>(maxLength: 256, nullable: true),
EmailConfirmed = table.Column<bool>(nullable: false),
PasswordHash = table.Column<string>(nullable: true),
SecurityStamp = table.Column<string>(nullable: true),
ConcurrencyStamp = table.Column<string>(nullable: true),
PhoneNumber = table.Column<string>(nullable: true),
PhoneNumberConfirmed = table.Column<bool>(nullable: false),
TwoFactorEnabled = table.Column<bool>(nullable: false),
LockoutEnd = table.Column<DateTimeOffset>(nullable: true),
LockoutEnabled = table.Column<bool>(nullable: false),
AccessFailedCount = table.Column<int>(nullable: false)
},

我不知道这些属性来自哪里,我想知道我是否可以改变这一点。我已经按照教程制作了一个网站,但现在我正在尝试自己尝试一个新项目。所以,问题是在使用AutoMapper时,我收到此错误消息,我想知道如何解决此问题:

自动映射程序为您创建了此类型映射,但无法使用当前配置映射您的类型。 帐户模型 -> 应用程序用户(目标成员列表) UserWebAPI.Models.AccountModel -> UserWebAPI.Models.ApplicationUser (Destination membership list)

未映射的属性: 编号 规范化用户名 规范化电子邮件 电子邮件已确认 密码哈希 防伪印章 并发标记 电话号码 电话号码已确认 双因素启用 锁定结束 锁定已启用 AccessFailedCount

帐户控制器.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using UserWebAPI.Models;
using AutoMapper;
using Microsoft.Extensions.Configuration;
namespace UserWebAPI.Controllers
{
public class AccountController : ControllerBase
{
private readonly IConfiguration _config;
private readonly IMapper _mapper;
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
public AccountController (IConfiguration config, 
IMapper mapper,
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager)
{
_userManager = userManager;
_signInManager = signInManager;
_mapper = mapper;
_config = config;
}
[Route("api/User/Register", Name = "GetUser") ]
[HttpPost]
public async Task<ActionResult> Register(AccountModel model) //add async Task<Result>
{
//var userStore = new UserStore<ApplicationUser>(new DataContext());
var userStore = _mapper.Map<ApplicationUser>(model);
//var manager = new UserManager<ApplicationUser>(userStore);
var manager = await _userManager.CreateAsync(userStore, model.Password);
var user = new ApplicationUser() { UserName = model.UserName, Email = model.Email };
//var user = _mapper.Map<ApplicationUser>(userStore);
user.FirstName = model.FirstName;
user.LastName = model.LastName;
if (manager.Succeeded)
{
//IdentityResult result = manager.Create(user, model.Password);
return CreatedAtRoute("GetUser", new { id = userStore.Id }, user);
}
return BadRequest(manager.Errors);
}
}
}

帐户模型.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace UserWebAPI.Models
{
public class AccountModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
}

身份模型.cs

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace UserWebAPI.Models
{
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class DataContext : IdentityDbContext<ApplicationUser> //DataContext instead of ApplicationDbContext
{
public DataContext(DbContextOptions<DataContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
//AspNetUsers -> User
builder.Entity<ApplicationUser>()
.ToTable("User");
//AspNetRoles -> Role
builder.Entity<IdentityRole>()
.ToTable("Role");
//AspNetRoles -> UserRole
builder.Entity<IdentityUserRole<string>>()
.ToTable("UserRole");
//AspNetUserClaims -> UserClaim
builder.Entity<IdentityUserClaim<string>>()
.ToTable("UserClaim");
//AspNetUserLogins -> UserLogin
builder.Entity<IdentityUserLogin<string>>()
.ToTable("UserLogin");
}
}
}

我想知道为什么当我运行命令进行新迁移时,它会创建一个具有许多不同属性的表,我没有指定。

由于您ApplicationUser继承了IdentityUser,因此当您进行迁移时,默认情况下它将使用这些属性创建表。您可以按 F12 在 vs(请参阅它继承的IdentityUser<string>)中检查IdentityUser模型。

另请参阅 ASP.NET 核心 中的标识模型自定义

MappingProfile.cs:

public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<AccountModel, ApplicationUser>();
}
}

最新更新