nhibernatefluent-nhibernatenhibernate-mappingfluent-nhibernate-mapping

Nhibernate added duplicate column on insert


I'm using Fluent NHibernate. I have a class called Audit which has ItemID property. Everytime that I want to save anything in that table give me this error.

Error message: The column name 'itemid' is specified more than once in the SET clause or column list of an INSERT. A column cannot be assigned more than one value in the same clause. Modify the clause to make sure that a column is updated only once. If this statement updates or inserts columns into a view, column aliasing can conceal the duplication in your code.

Classes

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() { }
}

Mapping:

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 Query:

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()


Solution

  • I fix this issue in my mapper.

    I changed

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

    to

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