自动映射:从null一对一属性中分配id

时间:2015-04-10 14:35:15

标签: c# nhibernate fluent-nhibernate automapper

我试图通过使用NHibernate,AutoMapper和Fluent来映射一个到一个类,但它对我不起作用。我收到此错误尝试从null一对一属性中分配id: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; }
}

这里是人和用户的流畅映射:

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对象?谁能让我朝着正确的方向前进?

1 个答案:

答案 0 :(得分:2)

我自己解决了这个问题。可能有其他方法可以解决这个问题,但这是我的方法。

我在创建忽略User属性(这是User模型的一个实例)的映射时告诉AutoMapper,当它像这样映射Person信息时;

AutoMapper.Mapper.CreateMap<Person, Models.Person>()
 .ForMember(x => x.User, opt => opt.Ignore());

然后我分别保存了人员和用户信息。

由于Person和User都具有一对一关系,并且其User表将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);
        }
    }

我希望这个答案可以帮助其他人