标签: nhibernate

TransactionScope如何回滚交易?

我正在编写一个集成测试,我将把一些对象插入数据库,然后检查以确定我的方法是否检索这些对象.

我与数据库的连接是通过NHibernate ...而我创建这样一个测试的常用方法是执行以下操作:

NHibernateSession.BeginTransaction();

//use nhibernate to insert objects into database
//retrieve objects via my method
//verify actual objects returned are the same as those inserted

NHibernateSession.RollbackTransaction();
Run Code Online (Sandbox Code Playgroud)

但是,我最近发现了TransactionScope,它显然可以用于这个目的......

我发现的一些示例代码如下:

public static int AddDepartmentWithEmployees(Department dept)
{

    int res = 0;

    DepartmentAdapter deptAdapter = new DepartmentAdapter();
    EmployeeAdapter empAdapter = new EmployeeAdapter();
    using (TransactionScope txScope = new TransactionScope())
    {

        res += deptAdapter.Insert(dept.DepartmentName);
        //Custom method made to return Department ID 
        //after inserting the department "Identity Column"
        dept.DepartmentID = deptAdapter.GetInsertReturnValue(); …
Run Code Online (Sandbox Code Playgroud)

.net c# nhibernate transactions transactionscope

95
推荐指数
2
解决办法
9万
查看次数

Criteria SpatialRestrictions.IsWithinDistance NHibernate.Spatial

有没有人实现过这个,或者知道是否难以实现这个/有任何指针?

public static SpatialRelationCriterion IsWithinDistance(string propertyName, object anotherGeometry, double distance)
{
    // TODO: Implement
    throw new NotImplementedException();
}
Run Code Online (Sandbox Code Playgroud)

来自NHibernate.Spatial.Criterion.SpatialRestrictions

我可以在hql中使用"NHSP.Distance(PROPERTY,:point)".但是想要将此查询与我现有的Criteria查询相结合.

目前我正在创建一个粗糙的多边形,并使用

criteria.Add(SpatialRestrictions.Intersects("PROPERTY", myPolygon));
Run Code Online (Sandbox Code Playgroud)

编辑 通过在SpatialRelationCriterion上重载构造函数来获得原型,添加新的SpatialRelation.Distance

public static SpatialRelationCriterion IsWithinDistance(string propertyName, object anotherGeometry, double distance)
        {
            return new SpatialRelationCriterion(propertyName, SpatialRelation.Distance, anotherGeometry, distance);
        }
Run Code Online (Sandbox Code Playgroud)

为SpatialRelationCriterion添加了一个新字段

private readonly double? distance;

public SpatialRelationCriterion(string propertyName, SpatialRelation relation, object anotherGeometry, double distance)
            : this(propertyName, relation, anotherGeometry)
        {
            this.distance = distance;
        }
Run Code Online (Sandbox Code Playgroud)

编辑ToSqlString

object secondGeometry = Parameter.Placeholder;
                if (!(this.anotherGeometry is IGeometry))
                {
                    secondGeometry = columns2[i];
                }

                if …
Run Code Online (Sandbox Code Playgroud)

.net nhibernate spatial geospatial criteria-api

95
推荐指数
0
解决办法
2903
查看次数

将IList或IEnumerable转换为Array的最佳方法

我有一个HQL查询,可以生成IList结果,或IEnumerable结果.

但是,我希望它返回我正在选择的实体数组,实现它的最佳方法是什么?我可以通过它枚举并构建数组,或者使用CopyTo()定义的数组.

有没有更好的方法?我选择了CopyTo方法.

c# nhibernate

93
推荐指数
4
解决办法
12万
查看次数

如何使用流畅的NHibernate将枚举映射为int值?

问题说真的,默认是它映射为a string但我需要它映射为int.

我目前正在使用PersistenceModel我的约定,如果这有任何区别.提前致谢.

更新 发现从主干上获取最新版本的代码解决了我的困境.

nhibernate fluent-nhibernate

87
推荐指数
4
解决办法
2万
查看次数

NHibernate中的反向属性

我如何使用反向属性?如果我没有弄错,对于一对多关系,inverse属性必须设置为true.对于多对多关系,其中一个实体类逆属性必须设置为true,另一个设置为false.

任何人都可以对此有所了解吗?

nhibernate nhibernate-mapping

87
推荐指数
2
解决办法
3万
查看次数

session.Merge和session.SaveOrUpdate有什么区别?

我有时会注意到我的父/子对象或多对多关系,我需要调用SaveOrUpdate或者Merge.通常,当我需要调用时,我调用SaveOrUpdate的异常Merge与不首先保存的瞬态对象有关.

请解释两者之间的区别.

nhibernate session

86
推荐指数
2
解决办法
9万
查看次数

JoinQueryOver和JoinAlias有什么区别?

我需要知道JoinQueryOver和JoinAlias之间的区别是什么,以及何时使用它们?

谢谢.

nhibernate queryover

86
推荐指数
2
解决办法
3万
查看次数

无法将NHibernate.Collection.Generic.PersistentGenericBag类型的对象强制转换为List

我有一个名为ReportRequest的类:

public class ReportRequest
{
    Int32 templateId;
    List<Int32> entityIds;

    public virtual Int32? Id
    {
        get;
        set;
    }

    public virtual Int32 TemplateId
    {
        get { return templateId; }
        set { templateId = value; }
    }

    public virtual List<Int32> EntityIds
    {
        get { return entityIds; }
        set { entityIds = value; }
    }

    public ReportRequest(int templateId, List<Int32> entityIds)
    {
        this.TemplateId = templateId;
        this.EntityIds = entityIds;
    }
}
Run Code Online (Sandbox Code Playgroud)

它使用Fluent Hibernate映射为:

public class ReportRequestMap : ClassMap<ReportRequest>
{
    public ReportRequestMap()
    {
        Id(x => x.Id).UnsavedValue(null).GeneratedBy.Native();
        Map(x …
Run Code Online (Sandbox Code Playgroud)

nhibernate nhibernate-mapping fluent-nhibernate

82
推荐指数
1
解决办法
3万
查看次数

如何在NHibernate中删除子对象?

我有一个父对象,它与子对象的IList有一对多的关系.删除子对象的最佳方法是什么?我没有删除父母.我的父对象包含一个IList子对象.以下是一对多关系的映射:

<bag name="Tiers" cascade="all">
  <key column="mismatch_id_no" />
  <one-to-many class="TGR_BL.PromoTier,TGR_BL"/>
</bag>
Run Code Online (Sandbox Code Playgroud)

如果我尝试使用clear()从集合中删除所有对象,然后调用SaveOrUpdate(),我会得到以下异常:

System.Data.SqlClient.SqlException: Cannot insert the value NULL into column
Run Code Online (Sandbox Code Playgroud)

如果我尝试单独删除子对象然后从父对象中删除它,我得到一个例外:

deleted object would be re-saved by cascade
Run Code Online (Sandbox Code Playgroud)

这是我第一次处理在NHibernate中删除子对象.我究竟做错了什么?

编辑:只是为了澄清 - 我不是试图删除父对象,只是删除子对象.我将父母的关系设置为一对多.我是否还需要在子对象映射上创建多对一关系?

nhibernate

78
推荐指数
1
解决办法
4万
查看次数

How to make a method generic when "type 'T' must be a reference type"?

Possible Duplicate:
Why do I get “error: … must be a reference type” in my C# generic method?

I have 2 Repository methods that are almost identical:

public IList<Fund> GetFundsByName(int pageSize, string searchExpression)
{
    return _session.CreateCriteria<Fund>()
        .AddNameSearchCriteria<Fund>(searchExpression)
        .AddOrder<Fund>(f => f.Name, Order.Asc)
        .SetMaxResults(pageSize).List<Fund>();
}

public IList<Company> GetCompaniesByName(int pageSize, string searchExpression)
{
    return _session.CreateCriteria<Company>()
        .AddNameSearchCriteria<Company>(searchExpression)
        .AddOrder<Company>(f => f.Name, Order.Asc)
        .SetMaxResults(pageSize).List<Company>();
}
Run Code Online (Sandbox Code Playgroud)

The only difference is that the first one's _session.CreateCriteria is of type Fund and the second one is company

I was …

c# generics nhibernate

78
推荐指数
2
解决办法
5万
查看次数