Automapper:一对一地从 null 分配 id 属性
Automapper: assign id from null one-to-one property
我正在尝试使用 NHibernate、AutoMapper 和 Fluent 进行一对一映射 类,但它对我不起作用。我收到此错误 attempted to assign id from null one-to-one 属性: Person
这是我的非模特 类:
public partial class Person
{
public int personId;
public string firstName;
public string lastName;
public int state;
public string emailAddress;
public User user;
}
public partial class User
{
public string Username {get; set;}
public string Password {get; set;}
public int UseLoginType {get; set}
}
模型:(为简单起见,我删除了不必要的属性)
public class Person
{
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string emailAddress; { get; set; }
public virtual int state; { get; set; }
public virtual User User { get; protected set; }
}
public class User {
protected User() { }
public User(Person person)
{
Person = Person;
}
public virtual string Username { get; set; }
public virtual string Password { get; set; }
public virtual int? UseLoginType { get; set; }
public virtual Person Person { get; protected set; }
}
这里是 Person 和 User 的流畅映射:
public PersonMap()
{
SetEntityProperties("Persons", "Person_ID");
Map(x => x.FirstName);
Map(x => x.LastName);
Map(x => x.State).CustomType(typeof(Int32));
HasOne(x => x.User).Cascade.All();
}
public UserMap()
{
Table("expense_Users");
Id(x => x.Id, "fkPersonId").GeneratedBy.Foreign("Person");
Map(x => x.Username);
Map(x => x.Password);
Map(x => x.UseLoginType);
HasOne(x => x.Person).Constrained().ForeignKey();
}
这就是我创建映射并尝试映射信息的方式:
AutoMapper.Mapper.CreateMap<Person, Models.Person>();
AutoMapper.Mapper.CreateMap<User, Models.User>();
foreach (var item in templateData.Persons)
{
save<Models.Person>(item);
}
private object save<TModel>(object templateObject)
{
var dbModel = AutoMapper.Mapper.Map<TModel>(templateObject);
return repository.Save<object>(dbModel);
}
谁能告诉我如何填充 Person 对象?谁能告诉我正确的方向?
我自己解决了这个问题。可能有其他方法可以解决这个问题,但这是我的方法。
我告诉 AutoMapper 在创建映射时忽略用户 属性(这是用户模型的一个实例),当它像这样映射 Person 信息时;
AutoMapper.Mapper.CreateMap<Person, Models.Person>()
.ForMember(x => x.User, opt => opt.Ignore());
然后我分别保存了Person和User信息。
因为 Person 和 User 都是一对一的关系,而它的 User table 持有 Person 主键作为数据库中的外键。因此,为了使用 Nhibernate 和 AutoMapping 解决这个问题,我使用了 AutoMapper Custom Resolver
来解析 Person 的引用,同时保存用户信息。下面的代码将向您清除此问题。
AutoMapper.Mapper.CreateMap<User, Models.User>()
.ForMember(x => x.Person, opt => opt.ResolveUsing(new personResolver(loadRepository)).FromMember(x => x.PersonId));
public class personResolver : ValueResolver<int, Models.Person>
{
private DatabaseLoadRepository loadRepository;
public personResolver(DatabaseLoadRepository repo)
{
this.loadRepository = repo;
}
protected override Models.Person ResolveCore(int personId)
{
return loadRepository.FindOne<Models.Person>(x => x.Id == personId);
}
}
希望这个回答对其他人有帮助
我正在尝试使用 NHibernate、AutoMapper 和 Fluent 进行一对一映射 类,但它对我不起作用。我收到此错误 attempted to assign id from null one-to-one 属性: Person
这是我的非模特 类:
public partial class Person
{
public int personId;
public string firstName;
public string lastName;
public int state;
public string emailAddress;
public User user;
}
public partial class User
{
public string Username {get; set;}
public string Password {get; set;}
public int UseLoginType {get; set}
}
模型:(为简单起见,我删除了不必要的属性)
public class Person
{
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string emailAddress; { get; set; }
public virtual int state; { get; set; }
public virtual User User { get; protected set; }
}
public class User {
protected User() { }
public User(Person person)
{
Person = Person;
}
public virtual string Username { get; set; }
public virtual string Password { get; set; }
public virtual int? UseLoginType { get; set; }
public virtual Person Person { get; protected set; }
}
这里是 Person 和 User 的流畅映射:
public PersonMap()
{
SetEntityProperties("Persons", "Person_ID");
Map(x => x.FirstName);
Map(x => x.LastName);
Map(x => x.State).CustomType(typeof(Int32));
HasOne(x => x.User).Cascade.All();
}
public UserMap()
{
Table("expense_Users");
Id(x => x.Id, "fkPersonId").GeneratedBy.Foreign("Person");
Map(x => x.Username);
Map(x => x.Password);
Map(x => x.UseLoginType);
HasOne(x => x.Person).Constrained().ForeignKey();
}
这就是我创建映射并尝试映射信息的方式:
AutoMapper.Mapper.CreateMap<Person, Models.Person>();
AutoMapper.Mapper.CreateMap<User, Models.User>();
foreach (var item in templateData.Persons)
{
save<Models.Person>(item);
}
private object save<TModel>(object templateObject)
{
var dbModel = AutoMapper.Mapper.Map<TModel>(templateObject);
return repository.Save<object>(dbModel);
}
谁能告诉我如何填充 Person 对象?谁能告诉我正确的方向?
我自己解决了这个问题。可能有其他方法可以解决这个问题,但这是我的方法。
我告诉 AutoMapper 在创建映射时忽略用户 属性(这是用户模型的一个实例),当它像这样映射 Person 信息时;
AutoMapper.Mapper.CreateMap<Person, Models.Person>()
.ForMember(x => x.User, opt => opt.Ignore());
然后我分别保存了Person和User信息。
因为 Person 和 User 都是一对一的关系,而它的 User table 持有 Person 主键作为数据库中的外键。因此,为了使用 Nhibernate 和 AutoMapping 解决这个问题,我使用了 AutoMapper Custom Resolver
来解析 Person 的引用,同时保存用户信息。下面的代码将向您清除此问题。
AutoMapper.Mapper.CreateMap<User, Models.User>()
.ForMember(x => x.Person, opt => opt.ResolveUsing(new personResolver(loadRepository)).FromMember(x => x.PersonId));
public class personResolver : ValueResolver<int, Models.Person>
{
private DatabaseLoadRepository loadRepository;
public personResolver(DatabaseLoadRepository repo)
{
this.loadRepository = repo;
}
protected override Models.Person ResolveCore(int personId)
{
return loadRepository.FindOne<Models.Person>(x => x.Id == personId);
}
}
希望这个回答对其他人有帮助