繁体   English   中英

NHibernate SaveOrUpdateCopy不会插入带有CompositeId的实体

[英]NHibernate SaveOrUpdateCopy won't insert entity with CompositeId

我有一个带有CompositeId的实体,该实体不会使用SaveOrUpdateCopy将新行插入数据库。 NHibernate生成的INSERT语句填充有“?” 每个领域的价值。 它使用SaveOrUpdate进行插入,使用SaveOrUpdateCopy或SaveOrUpdate进行更新,没有CompositeId的任何实体都通过SaveOrUpdateCopy进行插入/更新。 我不想创建一个if / then,然后使用CompositeId查找实体来决定是否应使用SaveOrUpdate或SaveOrUpdateCopy。 是否有一些技巧可以使SaveOrUpdateCopy与具有CompositeId的实体一起使用?

这是代码(为了保护无辜者而更改名称):

public class MyEntity
    {
        public virtual Int32 FirstProperty { get; set; }
        public virtual string SecondProperty { get; set; }
        public virtual string DataText { get; set; }

        public override int GetHashCode( )
        {
            int hashCode = 0;
            hashCode = hashCode ^ FirstProperty.GetHashCode() ^
                       SecondProperty.GetHashCode();
            return hashCode;
        }

        public override bool Equals( object obj )
        {
            MyEntity toCompare = obj as MyEntity;
            if( toCompare == null )
            {
                return false;
            }
            return ( GetHashCode() != toCompare.GetHashCode() );
        }
    }
public MyEntityMap()
        {
            CompositeId()
                .KeyProperty(x => x.FirstProperty, "first_property")
                .KeyProperty(x => x.SecondProperty, "second_property");

            Map(x => x.DataText, "data_text")
                .Nullable();

            Table("dbo.my_entity");
        }

数据库调用:

public MyEntity GetMyEntity(long firstProperty, string secondProperty)
        {
            using (var session = sessionFactory.OpenSession())
            {
                var result = from entity in
                                session.Linq()
                            where entity.FirstProperty == firstProperty
                                  && entity.SecondProperty== secondProperty
                            select entity;
                return result.Count() > 0 ? result.First() : null;
            }
        }

数据库保存:

using (var session = sessionFactory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    try
                    {
                        session.SaveOrUpdateCopy(entity);
                        transaction.Commit();
                    }
                    catch(Exception ex)
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }

将版本属性添加到组合键类,有关详细说明,请参阅本文

嗨,我在FluentnHibernate中使用了CompositeId,但是我对EqualsGetHashCode的实现却不同。 这一类有7个字段作为键:

public class PresupuestoGastoPromocion

{
    public PresupuestoGastoPromocion() { }

    public virtual int Año { get; set; }
    public virtual int Mes { get; set; }
    public virtual int PartidaId { get; set; }
    public virtual string Equipo { get; set; }
    public virtual string CodFamilia { get; set; }
    public virtual string GDP { get; set; }
    public virtual int TipoPresupuestoId { get; set; }
    public virtual float Monto { get; set; }
    public override bool Equals(object obj)
    {
        if (obj == null)
            return false;
        var t = obj as PresupuestoGastoPromocion;
        if (t == null)
            return false;
        if (CodFamilia == t.CodFamilia && Año == t.Año && Mes == t.Mes && TipoPresupuestoId == t.TipoPresupuestoId && Equipo == t.Equipo && PartidaId == t.PartidaId && GDP == t.GDP)
            return true;
        return false;
    }
    public override int GetHashCode()
    {
        return (CodFamilia + "|" + Año + "|" + Mes + "|" + TipoPresupuestoId + "|" + Equipo + "|" + PartidaId + "|" + GDP).GetHashCode();
    }

}



public class PresupuestoGastoPromocionMap : ClassMap<PresupuestoGastoPromocion>
{
    public PresupuestoGastoPromocionMap()
    {
        Table("PresupuestoGastoPromocion");
        CompositeId()
            .KeyProperty(x => x.Año)
            .KeyProperty(x => x.Mes)
            .KeyProperty(x => x.TipoPresupuestoId)
            .KeyProperty(x => x.CodFamilia, "Cod_Familia")
            .KeyProperty(x => x.Equipo)
            .KeyProperty(x => x.GDP)
            .KeyProperty(x => x.PartidaId);

        Map(x => x.Monto).Column("Monto");


    }
}

我希望这能帮到您。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM