I have two classes from example using Fluent NHibernate mapping. Fluent NHibernate mapping is commented and I´m trying to make Code Based Mapping, but still appears ArgumentNullException "Value cannot be null.". How to make it right?
//Fluent NHIbernate mapping for table LocalizationEntry
//public class LocalizationEntryMapping : ClassMap<LocalizationEntry>
//{
// public LocalizationEntryMapping()
// {
// Cache.ReadWrite();
// CompositeId()
// .ComponentCompositeIdentifier(x => x.Id)
// .KeyProperty(x => x.Id.Culture)
// .KeyProperty(x => x.Id.EntityId)
// .KeyProperty(x => x.Id.Property)
// .KeyProperty(x => x.Id.Type);
// Map(x => x.Message);
// }
//}
public class LocalizationEntryId
{
public virtual string Culture { get; set; }
public virtual string Type { get; set; }
public virtual string Property { get; set; }
public virtual string EntityId { get; set; }
public override bool Equals(object obj)
{
if (obj != null)
{
LocalizationEntryId other = obj as LocalizationEntryId;
if (other != null)
{
return this.Type == other.Type &&
this.Property == other.Property &&
this.EntityId == other.EntityId &&
this.Culture == other.Culture;
}
}
return false;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
public class LocalizationEntry : IDomainMapper
{
public virtual LocalizationEntryId Id { get; set; }
public virtual string Message { get; set; }
public virtual void Map(ModelMapper mapper)
{
mapper.Class<LocalizationEntry>(m =>
{
m.ComposedId( t =>
{
t.Property(g => g.Id.Culture, c =>
{
c.NotNullable(true);
c.Length(10);
});
t.Property(g => g.Id.EntityId, c =>
{
c.NotNullable(true);
});
t.Property(g => g.Id.Property, c =>
{
c.NotNullable(true);
c.Length(100);
});
t.Property(g => g.Id.Type, c =>
{
c.NotNullable(true);
c.Length(100);
});
});
m.Property(t => t.Message, c =>
{
c.NotNullable(true);
c.Length(400);
});
});
}
}
//////////// EDIT //////////////// I found the solution already. Mapping should be done this way:
public virtual void Map(ModelMapper mapper)
{
mapper.Class<LocalizationEntry>(m =>
{
m.ComponentAsId(x => x.Id, n =>
{
n.Property(x => x.Culture);
n.Property(x => x.EntityId);
n.Property(x => x.Property);
n.Property(x => x.Type);
});
m.Property(t => t.Message, c =>
{
c.NotNullable(true);
c.Length(400);
});
});
}