Nhibernate 在插入时添加了重复的列
Nhibernate added duplicate column on insert
我正在使用 Fluent NHibernate。我有一个名为 Audit 的 class,它具有 ItemID 属性。每次我想在 table 中保存任何内容时,都会给我这个错误。
错误信息:
在 INSERT 的 SET 子句或列列表中多次指定列名 'itemid'。在同一子句中不能为一列分配多个值。修改子句以确保列只更新一次。如果此语句更新列或将列插入视图,列别名可以隐藏代码中的重复项。
类
public class Audit : EntityBase
{
/*********
** Accessors
*********/
/// <summary>When the action was logged.</summary>
public virtual DateTime Date { get; set; }
/// <summary>The person who triggered the action.</summary>
public virtual BasicUser User { get; set; }
/// <summary>The name of the related group, used for filtering (e.g. idea, system-settings, site-editor)</summary>
public virtual string ItemGroup { get; set; }
/// <summary>The ID of the related entity.</summary>
public virtual int ItemID { get; set; }
/// <summary>The title of the related entity.</summary>
public virtual LocalizableString ItemTitle { get; set; }
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
public Audit() { }
}
映射:
public class AuditMapper<T> : IEntityMapper<T>
where T : Audit
{
public virtual void ExtendDomainModel(IEntityMetadata<T> metadata)
{
metadata.MapTablePerClassHierarchy<Audit, T>("`ItemGroup`");
if (!metadata.IsSubclass)
{
metadata.Map.ManyToOne(entity => entity.User, relation => relation.Column("`userid`"));
metadata.Map.Property(c => c.ItemGroup, mapper =>
{
mapper.Insert(false);
mapper.Update(false);
});
metadata.Map.Property(c => c.ItemID, mapper => mapper.Column("itemid"));
}
else
{
metadata.MapSubclass.ManyToOne(entity => entity.User, relation => relation.Column("`userid`"));
metadata.MapSubclass.Property(c => c.ItemGroup, mapper =>
{
mapper.Insert(false);
mapper.Update(false);
});
metadata.MapSubclass.Property(c => c.ItemID, mapper =>
{
mapper.Insert(false);
mapper.Update(false);
});
}
}
}
SQL 查询:
INSERT INTO [brainbank].[idealink.core.audit] ([date], [userid], itemid, [itemtitle_key], [itemtitle_original], [itemid], [ ItemGroup]) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, 'IdeaAudit'); select SCOPE_IDENTITY()
我在我的映射器中修复了这个问题。
我改了
metadata.Map.Property(c => c.ItemID, mapper => mapper.Column("itemid"));
到
metadata.Map.Property(c => c.ItemID, mapper =>
{
mapper.Insert(false);
mapper.Update(false);
});
我正在使用 Fluent NHibernate。我有一个名为 Audit 的 class,它具有 ItemID 属性。每次我想在 table 中保存任何内容时,都会给我这个错误。
错误信息: 在 INSERT 的 SET 子句或列列表中多次指定列名 'itemid'。在同一子句中不能为一列分配多个值。修改子句以确保列只更新一次。如果此语句更新列或将列插入视图,列别名可以隐藏代码中的重复项。
类
public class Audit : EntityBase
{
/*********
** Accessors
*********/
/// <summary>When the action was logged.</summary>
public virtual DateTime Date { get; set; }
/// <summary>The person who triggered the action.</summary>
public virtual BasicUser User { get; set; }
/// <summary>The name of the related group, used for filtering (e.g. idea, system-settings, site-editor)</summary>
public virtual string ItemGroup { get; set; }
/// <summary>The ID of the related entity.</summary>
public virtual int ItemID { get; set; }
/// <summary>The title of the related entity.</summary>
public virtual LocalizableString ItemTitle { get; set; }
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
public Audit() { }
}
映射:
public class AuditMapper<T> : IEntityMapper<T>
where T : Audit
{
public virtual void ExtendDomainModel(IEntityMetadata<T> metadata)
{
metadata.MapTablePerClassHierarchy<Audit, T>("`ItemGroup`");
if (!metadata.IsSubclass)
{
metadata.Map.ManyToOne(entity => entity.User, relation => relation.Column("`userid`"));
metadata.Map.Property(c => c.ItemGroup, mapper =>
{
mapper.Insert(false);
mapper.Update(false);
});
metadata.Map.Property(c => c.ItemID, mapper => mapper.Column("itemid"));
}
else
{
metadata.MapSubclass.ManyToOne(entity => entity.User, relation => relation.Column("`userid`"));
metadata.MapSubclass.Property(c => c.ItemGroup, mapper =>
{
mapper.Insert(false);
mapper.Update(false);
});
metadata.MapSubclass.Property(c => c.ItemID, mapper =>
{
mapper.Insert(false);
mapper.Update(false);
});
}
}
}
SQL 查询:
INSERT INTO [brainbank].[idealink.core.audit] ([date], [userid], itemid, [itemtitle_key], [itemtitle_original], [itemid], [ ItemGroup]) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, 'IdeaAudit'); select SCOPE_IDENTITY()
我在我的映射器中修复了这个问题。
我改了
metadata.Map.Property(c => c.ItemID, mapper => mapper.Column("itemid"));
到
metadata.Map.Property(c => c.ItemID, mapper =>
{
mapper.Insert(false);
mapper.Update(false);
});