Nhibernate在插入时添加了重复列

时间:2018-07-27 13:27:58

标签: nhibernate fluent-nhibernate nhibernate-mapping fluent-nhibernate-mapping

我正在使用Fluent NHibernate。我有一个名为Audit的类,该类具有ItemID属性。每当我想在该表中保存任何内容时,都会出现此错误。

错误消息: 列名“ itemid”在INSERT的SET子句或列列表中多次指定。一列不能在同一子句中分配多个值。修改该子句以确保一列仅更新一次。如果此语句更新视图或在视图中插入列,则列别名会隐藏代码中的重复项。

课程

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查询:

插入[brainbank]。[idealink.core.audit]([日期],[用户ID],itemid,[itemtitle_key],[itemtitle_original],[itemid],[ItemGroup])值(@ p0,@ p1) ,@ p2,@ p3,@ p4,@ p5,'IdeaAudit');选择SCOPE_IDENTITY()

1 个答案:

答案 0 :(得分:0)

我已在我的映射器中解决了此问题。

我改变了

        metadata.Map.Property(c => c.ItemID, mapper => mapper.Column("itemid"));

        metadata.Map.Property(c => c.ItemID, mapper =>
                {
                    mapper.Insert(false);
                    mapper.Update(false);
                });