Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自动映射异常_C#_Automapper - Fatal编程技术网

C# 自动映射异常

C# 自动映射异常,c#,automapper,C#,Automapper,当我运行代码Mapper.Map(Account,User)行时;我得到一个“缺少类型映射配置或不支持的映射”异常。我还想指出,line Mapper.Map(Account);不会引发异常并返回预期结果。我试图做的是在不创建新的User实例的情况下,将值从一个帐户移动到另一个用户。任何帮助都会很好。谢谢 public class AccountUpdate { [Email] [Required] public string Email { get; set; }

当我运行代码Mapper.Map(Account,User)行时;我得到一个“缺少类型映射配置或不支持的映射”异常。我还想指出,line Mapper.Map(Account);不会引发异常并返回预期结果。我试图做的是在不创建新的User实例的情况下,将值从一个帐户移动到另一个用户。任何帮助都会很好。谢谢

public class AccountUpdate
{
    [Email]
    [Required]
    public string Email { get; set; }

    [Required]
    [StringLength(25, MinimumLength = 3, ErrorMessage = "Your name must be between 3 and 25 characters")]
    public string Name { get; set; }

    public string Roles { get; set; }
}

public class User
{
    public User()
    {
        Roles = new List<Role>();
    }
    public int UserId { get; set; }
    public string Email { get; set; }
    public string Name { get; set; }
    public byte[] Password { get; set; }
    public byte[] Salt { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime LastLogin { get; set; }
    public virtual ICollection<Role> Roles { get; set; }
}


Mapper.CreateMap<AccountUpdate, User>().ForMember(d => d.Roles, s => s.Ignore());
公共类帐户更新
{
[电邮]
[必需]
公共字符串电子邮件{get;set;}
[必需]
[StringLength(25,MinimumLength=3,ErrorMessage=“您的姓名必须介于3到25个字符之间”)]
公共字符串名称{get;set;}
公共字符串角色{get;set;}
}
公共类用户
{
公共用户()
{
角色=新列表();
}
public int UserId{get;set;}
公共字符串电子邮件{get;set;}
公共字符串名称{get;set;}
公共字节[]密码{get;set;}
公共字节[]Salt{get;set;}
public DateTime CreatedOn{get;set;}
public DateTime LastLogin{get;set;}
公共虚拟ICollection角色{get;set;}
}
Mapper.CreateMap().formMember(d=>d.Roles,s=>s.Ignore());

您没有映射目标类的所有成员


调用
Mapper.assertconfigurationsvalid()有关该问题的详细信息:

Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
==============================================================
AccountUpdate -> User (Destination member list)
ConsoleApplication1.AccountUpdate -> ConsoleApplication1.User (Destination member list)
--------------------------------------------------------------
UserId
Password
Salt
CreatedOn
LastLogin
要解决此问题,请显式忽略未映射的成员


我刚刚测试了这个:

Mapper.CreateMap<AccountUpdate, User>()
        .ForMember(d => d.Roles, s => s.Ignore())
        .ForMember(d => d.UserId, s => s.Ignore())
        .ForMember(d => d.Password, s => s.Ignore())
        .ForMember(d => d.Salt, s => s.Ignore())
        .ForMember(d => d.CreatedOn, s => s.Ignore())
        .ForMember(d => d.LastLogin, s => s.Ignore());

Mapper.AssertConfigurationIsValid();

var update = new AccountUpdate
{
    Email = "foo@bar.com",
    Name = "The name",
    Roles = "not important"
};

var user = Mapper.Map<AccountUpdate, User>(update);

Trace.Assert(user.Email == update.Email);
Trace.Assert(user.Name == update.Name);

您没有映射目标类的所有成员


调用
Mapper.assertconfigurationsvalid()有关该问题的详细信息:

Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
==============================================================
AccountUpdate -> User (Destination member list)
ConsoleApplication1.AccountUpdate -> ConsoleApplication1.User (Destination member list)
--------------------------------------------------------------
UserId
Password
Salt
CreatedOn
LastLogin
要解决此问题,请显式忽略未映射的成员


我刚刚测试了这个:

Mapper.CreateMap<AccountUpdate, User>()
        .ForMember(d => d.Roles, s => s.Ignore())
        .ForMember(d => d.UserId, s => s.Ignore())
        .ForMember(d => d.Password, s => s.Ignore())
        .ForMember(d => d.Salt, s => s.Ignore())
        .ForMember(d => d.CreatedOn, s => s.Ignore())
        .ForMember(d => d.LastLogin, s => s.Ignore());

Mapper.AssertConfigurationIsValid();

var update = new AccountUpdate
{
    Email = "foo@bar.com",
    Name = "The name",
    Roles = "not important"
};

var user = Mapper.Map<AccountUpdate, User>(update);

Trace.Assert(user.Email == update.Email);
Trace.Assert(user.Name == update.Name);

项目中记录了一个问题,概述了一个潜在的解决方案,即使用源对象作为映射,而不是目标。和相关的代码示例为:

CreateMap<Source, Dest>(MemberList.Source)
CreateMap(MemberList.Source)

还有一种更通用的扩展方法是有人在链接问题中编写的,这也可能有助于节省时间。

项目中记录了一个问题,概述了一个潜在的解决方案,即使用源对象作为映射,而不是目标。和相关的代码示例为:

CreateMap<Source, Dest>(MemberList.Source)
CreateMap(MemberList.Source)

还有一个更通用的扩展方法是有人在链接的问题中编写的,这也可能有助于节省时间。

我尝试显式忽略未映射的成员,但调用Mapper.assertconfigurationsvalid()时;我得到了上面列出的同样的东西。我的配置现在看起来是这样的:Mapper.CreateMap().ForMember(d=>d.Roles,s=>s.Ignore()).ForMember(d=>d.UserId,s=>s.Ignore()).ForMember(d=>d.Password,s=>s.Ignore()).ForMember(d=>d.Salt,s=>s.Ignore()).ForMember(d=>d.CreatedOn,s=>s=>s.Ignore()).ForMember(d=>d.LastLogin,s=>s.Ignore());assertConfigurationsValid();不再抛出异常和类似var user=Mapper.Map(更新)的调用;将工作,但当我调用var b=Mapper.Map(Account,User)时;我仍然收到一个异常。你能发布完整的异常消息吗?是否确定传递给
Map
的对象属于已定义映射的类型?而不是代理、集合等?该对象是代理,因为它是由DbContext生成的。我将把它扔到那里,我讨厌AutoTrapperi尝试显式忽略未映射的成员,但当我调用Mapper.AssertConfigurationsValid()时;我得到了上面列出的同样的东西。我的配置现在看起来是这样的:Mapper.CreateMap().ForMember(d=>d.Roles,s=>s.Ignore()).ForMember(d=>d.UserId,s=>s.Ignore()).ForMember(d=>d.Password,s=>s.Ignore()).ForMember(d=>d.Salt,s=>s.Ignore()).ForMember(d=>d.CreatedOn,s=>s=>s.Ignore()).ForMember(d=>d.LastLogin,s=>s.Ignore());assertConfigurationsValid();不再抛出异常和类似var user=Mapper.Map(更新)的调用;将工作,但当我调用var b=Mapper.Map(Account,User)时;我仍然收到一个异常。你能发布完整的异常消息吗?是否确定传递给
Map
的对象属于已定义映射的类型?而不是代理、集合等?对象是代理,因为它是由DbContext生成的。我要把它扔出去,我讨厌Autocraper