Я создаю небольшое приложение с использованием Sharp Architecture и столкнулся с ошибкой, которую не могу понять. Я думаю, что это как-то связано с отображениями NHibernte. В моем методе HttpPost Create () мой вызов SaveOrUpdate пытается вставить ноль в поле первичного ключа таблицы. Объявление для первичного ключа в моей модели: public virtual int Id { get; protected set; }
.
Я проверил newSprint.Id, и он равен нулю. Я думаю, что проблема с моими сопоставлениями NHibernate, поэтому я включил все это ниже.
Вот конфигурация автоподстановки:
public class AutomappingConfiguration : DefaultAutomappingConfiguration
{
public override bool ShouldMap(System.Type type)
{
return type.GetInterfaces().Any(x =>
x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEntityWithTypedId<>));
}
public override bool ShouldMap(Member member)
{
return base.ShouldMap(member) && member.CanWrite;
}
public override bool AbstractClassIsLayerSupertype(System.Type type)
{
return type == typeof(EntityWithTypedId<>) || type == typeof(Entity);
}
public override bool IsId(Member member)
{
return member.Name == "Id";
}
}
Генератор модели автоматического сохранения:
public class AutoPersistenceModelGenerator : IAutoPersistenceModelGenerator
{
public AutoPersistenceModel Generate()
{
var mappings = AutoMap.AssemblyOf<Sprint>(new AutomappingConfiguration());
mappings.IgnoreBase<Entity>();
mappings.IgnoreBase(typeof(EntityWithTypedId<>));
mappings.Conventions.Setup(GetConventions());
mappings.UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();
return mappings;
}
private static Action<IConventionFinder> GetConventions()
{
return c =>
{
c.Add<PrimaryKeyConvention>();
c.Add<CustomForeignKeyConvention>();
c.Add<HasManyConvention>();
c.Add<TableNameConvention>();
};
}
Заранее благодарен за любую помощь, которую может предложить каждый.
Редактировать
Я выяснил, что проблема была в соглашении об именах таблиц. Удаление этого из конфигурации AutoMapping решило проблему. Я удалил посторонний код и добавил отображение TableNameConvention в надежде, что кто-нибудь сможет объяснить, что именно вызвало эти проблемы.
public class TableNameConvention : IClassConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.IClassInstance instance)
{
instance.Table(Inflector.Net.Inflector.Pluralize(instance.EntityType.Name));
}
}