我有两个使用 Fluent NHibernate 映射的示例类。Fluent NHibernate 映射已被注释,我正在尝试进行基于代码的映射,但仍然出现 ArgumentNullException "Value cannot be null."。如何使它正确?
//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);
});
});
}
}
/////////// 编辑 /////////////// 我已经找到了解决方案。映射应该以这种方式完成:
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);
});
});
}