2014-11-25 3 views
0

가 여기 내 설정의"대상 유형과 일치하지 않습니다NHibernate에 "오류에 대한 ... 속성 값을 탈수"... "개체가

클래스/인터페이스 :

IEntity 
User : IEntity 
Vehicle : IEntity 

나는에 대한 유창함 자 NHibernate를 사용하고 있습니다 설정.

내가하려는 것은 모든 Entity 클래스에 Created, Creator, Updated 및 Updater를 저장하는 것입니다.

public class NHibernateListener : IPreInsertEventListener, IPreUpdateEventListener 
{ 
    private static readonly string CreatedPropertyName = GetPropertyName<IEntity>(val => val.Created); 
    private static readonly string CreatorPropertyName = GetPropertyName<IEntity>(val => val.Creator); 
    private static readonly string UpdatedPropertyName = GetPropertyName<IEntity>(val => val.Updated); 
    private static readonly string UpdaterPropertyName = GetPropertyName<IEntity>(val => val.Updater); 

    public bool OnPreInsert(PreInsertEvent @event) 
    { 
     IEntity entity; 
     if ((entity = @event.Entity as IEntity) != null) 
     { 
      var currentDate = DateTime.Now; 
      entity.Created = entity.Updated = currentDate; 

      if (User.CurrentUser != null) 
      { 
       entity.Creator = entity.Updater = User.CurrentUser; 
       Set(@event.Persister, @event.State, CreatorPropertyName, currentDate); 
       Set(@event.Persister, @event.State, UpdaterPropertyName, currentDate); 
      } 

      Set(@event.Persister, @event.State, CreatedPropertyName, currentDate); 
      Set(@event.Persister, @event.State, UpdatedPropertyName, currentDate); 
     } 
     return false; 
    } 

    public bool OnPreUpdate(PreUpdateEvent @event) 
    { 
     IEntity entity; 
     if ((entity = @event.Entity as IEntity) != null) 
     { 
      var currentDate = DateTime.Now; 
      entity.Updated = currentDate; 

      if (User.CurrentUser != null) 
      { 
       entity.Creator = entity.Updater = User.CurrentUser; 
       Set(@event.Persister, @event.State, UpdaterPropertyName, currentDate); 
      } 

      Set(@event.Persister, @event.State, UpdatedPropertyName, currentDate); 
     } 
     return false; 
    } 

    private void Set(IEntityPersister persister, object[] state, string propertyName, object value) 
    { 
     var index = Array.IndexOf(persister.PropertyNames, propertyName); 
     if (index == -1) 
      return; 
     state[index] = value; 
    } 
    private static string GetPropertyName<TType>(Expression<Func<TType, object>> expression) 
    { 
     return ExpressionProcessor.FindPropertyExpression(expression.Body); 
    } 

} 

CreatedUpdated 잘 작동 :

나는이 작업을 수행하려면 다음 NHibernate에 청취자가 있습니다. 작성자 및 업데이터가 작동하지 않습니다.

클래스

IEntity

public enum Status 
{ 
    Active, 
    Inactive 
} 

public interface IEntity 
{ 
    Guid Id { get; } 
    DateTime Created { get; set; } 
    User Creator { get; set; } 
    DateTime Updated { get; set; } 
    User Updater { get; set; } 
    Status Status { get; set; } 

} 

사용자 - 유형 User이다 CurrentUser라는 정적 Property이 있습니다.

public class User : IEntity 
{ 
    public static User CurrentUser { get; set; } 

    public virtual string Email { get; protected set; } 
    public virtual Guid Id { get; protected set; } 
    public virtual DateTime Created { get; set; } 
    public virtual User Creator { get; set; } 
    public virtual DateTime Updated { get; set; } 
    public virtual User Updater { get; set; } 
    public virtual Status Status { get; set; } 
    public virtual string FirstName { get; protected set; } 
    public virtual string LastName { get; protected set; } 
    public virtual IEnumerable<Vehicle> Vehicles { get; protected set; } 

    protected User() 
    {    
    } 

    public User(Guid id, string email, string firstName, string lastName) 
    { 
     Id = id; 
     FirstName = firstName; 
     LastName = lastName; 
     Email = email; 
    } 

    public virtual void AddVehicle(Vehicle vehicle) 
    { 
     ((IList<Vehicle>) Vehicles).Add(vehicle); 
     vehicle.AddUser(this); 
    } 
} 

나는이 두 클래스에 대한 재 지정으로, 자동 매핑을 사용하고

public class Vehicle : IEntity 
{ 
    protected Vehicle() 
    { 
    } 

    public Vehicle(Guid id, string make, string model, string color) 
    { 
     Id = id; 
     Make = make; 
     Model = model; 
     Color = color; 
     Users = new List<User>(); 
    } 

    public virtual string Make{ get; set; } 
    public virtual string Model{ get; set; } 
    public virtual string Color { get; set; } 
    public virtual IEnumerable<User> Users { get; protected set; } 
    public virtual Guid Id { get; set; } 
    public virtual DateTime Created { get; set; } 
    public virtual User Creator { get; set; } 
    public virtual DateTime Updated { get; set; } 
    public virtual User Updater { get; set; } 
    public virtual Status Status { get; set; } 

    protected internal virtual void AddUser(User user) 
    { 
     ((IList<User>) Users).Add(user); 
    } 
} 

차량.

내가 트랜잭션이 커밋 할 때 나는 다음과 같은 오류가 실행

Message: Error dehydrating property value for MyApp.Domain.Users.User.Creator 

Inner Exception: Exception occurred getter of MyApp.Domain.Users.User.Id 

Inner Inner Exception: Object does not match target type. 

답변

0

두 라인은 나에게 큰 보이지 않는이 :

 Set(@event.Persister, @event.State, CreatorPropertyName, currentDate); 
     Set(@event.Persister, @event.State, UpdaterPropertyName, currentDate); 

그들은 심지어이 있어야한다?