C# 无法在框架4.7.1中初始化自动映射
在LoginService中调用方法C# 无法在框架4.7.1中初始化自动映射,c#,automapper,C#,Automapper,在LoginService中调用方法并尝试ProjectTo时,给我异常映射程序未初始化。使用适当的配置调用initialize……我尝试使用服务。AddAutoMapper像NetCore一样,因为有工作,但在NetFramework中找不到他。在NetFramework中最简单的方法是什么。我读了另一个老答案,但觉得对我来说太复杂了。当然,我不能将此方法用于ProjectTo,但是 Startup.cs IServiceProvider service = ConfigureServices
并尝试ProjectTo
时,给我异常映射程序未初始化。使用适当的配置调用initialize……我尝试使用服务。AddAutoMapper
像NetCore一样,因为有工作,但在NetFramework中找不到他。在NetFramework中最简单的方法是什么。我读了另一个老答案,但觉得对我来说太复杂了。当然,我不能将此方法用于ProjectTo,但是
Startup.cs
IServiceProvider service = ConfigureServices();
var config = new MapperConfiguration(cfg => {
cfg.AddProfile(service.GetService<DailyReporterProfiler>());
});
private static IServiceProvider ConfigureServices()
{
IServiceCollection services = new ServiceCollection();
services.AddTransient<ICommandInterpreter, CommandInterpreter>();
services.AddTransient<IValidation, Validation>();
#region Services
services.AddTransient<ILoginService, LoginService>();
#endregion
services.AddDbContext<DailyReportContext>(options =>
options.UseSqlServer(Configuration.ConnectionString));
services.AddSingleton<DailyReporterProfiler>();
var result = services.BuildServiceProvider();
return result;
}
IServiceProvider服务=配置服务();
var config=new-MapperConfiguration(cfg=>{
AddProfile(service.GetService());
});
专用静态IServiceProvider配置服务()
{
IServiceCollection服务=新的ServiceCollection();
services.AddTransient();
services.AddTransient();
#区域服务
services.AddTransient();
#端区
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.ConnectionString));
services.AddSingleton();
var result=services.BuildServiceProvider();
返回结果;
}
剖析器
public class DailyReporterProfiler : Profile
{
public DailyReporterProfiler()
{
var configuration = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Account, AccountLoginDTO>().ReverseMap();
});
}
}
公共类DailReporterProfiler:配置文件
{
公共DailReporterProfiler()
{
var配置=新的MapperConfiguration(cfg=>
{
CreateMap().ReverseMap();
});
}
}
LoginService.cs
public class LoginService : ILoginService
{
private DailyReportContext context;
public LoginService(DailyReportContext context)
{
this.context = context;
}
public TModel ByUsername<TModel>(string username) => By<TModel>(u => u.Username == username)
.SingleOrDefault();
public string GetAccountStatus(string username)
{
var account = this.context.Accounts.FirstOrDefault(u => u.Username == username).Status;
var status = Enum.GetName(typeof(AccountStatus), account);
return status;
}
public void ChangePassword(int userId, string password)
{
throw new NotImplementedException();
}
public bool CheckPassword(string username, string password)
{
var account = context.Accounts.FirstOrDefault(u => u.Username == username);
if (account.Password != password)
{
return false;
}
return true;
}
public bool Exists(string username) => ByUsername<Account>(username) != null;
public IEnumerable<TModel> By<TModel>(Func<Account, bool> predicate)
{
return this.context.Accounts.Where(predicate).AsQueryable().ProjectTo<TModel>();
}
}
公共类登录服务:ILoginService
{
私有DailyReportContext上下文;
公共登录服务(DailyReportContext上下文)
{
this.context=上下文;
}
公共tModelByUserName(字符串用户名)=>By(u=>u.username==username)
.SingleOrDefault();
公共字符串GetAccountStatus(字符串用户名)
{
var account=this.context.Accounts.FirstOrDefault(u=>u.Username==Username).Status;
var status=Enum.GetName(typeof(AccountStatus),account);
返回状态;
}
public void ChangePassword(int userId,字符串密码)
{
抛出新的NotImplementedException();
}
public bool CheckPassword(字符串用户名、字符串密码)
{
var account=context.Accounts.FirstOrDefault(u=>u.Username==Username);
如果(account.Password!=密码)
{
返回false;
}
返回true;
}
public bool Exists(字符串用户名)=>ByUsername(用户名)!=null;
公共IEnumerable By(Func谓词)
{
返回此.context.Accounts.Where(谓词).AsQueryable().ProjectTo();
}
}
不要帮我,但要感谢你,因为你应该从一个简单的例子开始,一步步向上。