小编Jag*_*uar的帖子

IndexOutOfRangeException在NHibernate的深处

我有以下映射:

public class SecurityMap : ClassMap<Security>
    {
        public SecurityMap()
        {
            Table("Security");
            CompositeId().KeyProperty(k => k.Id, "SecurityId").KeyProperty(k => k.EndDate);
            Map(x => x.LastUpdateUser);
            References(x => x.Company).Columns("CompanyId", "EndDate");
            References(x => x.PrimaryListing).Columns("PrimaryListingId", "EndDate");
         }
    }

public class ListingMap : ClassMap<Listing>
    {
        public ListingMap()
        {
            Table("Listing");
            CompositeId().KeyProperty(k => k.Id, "ListingID").KeyProperty(k => k.EndDate);
            References(x => x.Security).Columns("SecurityId","EndDate");
        }
    }

 public class CompanyMap : ClassMap<Company>
    {
        public CompanyMap()
        {
            Table("Company");
            CompositeId().KeyProperty(k => k.Id, "CompanyID").KeyProperty(k => k.EndDate);
            HasMany(x => x.Securities).KeyColumns.Add("CompanyId", "EndDate");
        }       
    }
Run Code Online (Sandbox Code Playgroud)

当我尝试运行此测试时:

[Test]
public void can_update_a_security()
{
    var …
Run Code Online (Sandbox Code Playgroud)

mapping nhibernate exception nhibernate-mapping fluent-nhibernate

12
推荐指数
1
解决办法
8212
查看次数

强制执行查询而不刷新/提交

您好我正在使用asp.net Web应用程序的每个请求事务(视图会话)模式.我在应用程序中有几个要点,我想保存一个NHibernate管理实体,然后使用常见的SQL执行几个插入和更新.这些插入/更新取决于NH保存实体将采用的ID.

问题是生成的id在事务范围内不存在.如果我强制刷新/提交id是持久的,但如果插入/更新失败,我必须回滚,但刷新/提交的实体不会.目前我正在为这些案例进行手动插入,但这是我想要改变的.那么,有没有办法在Save()之后执行SQL语句(在已打开的事务中)但不强制刷新/提交?

编辑:我正在添加一个半伪代码示例,我有4个错误的答案所以我认为人们不理解(NHibernate如何工作)在开始请求我发出一个

nhsession.BeginTransaction()
Run Code Online (Sandbox Code Playgroud)

然后在某些时候我做

FooClass fc = new FooClass("value");
nhsession.Save(fc);
ITransaction trans = nhsession.Transaction;
SqlCommand sc = new SqlCommand("some insert/update query that depends on fc's id", (SqlConnection)nhsession.Connection);
sc.Parameters.Add("id", fc.Id); //NHibernate generates the id, note i'm using assigned/hi-lo so no round trip to the db takes place
transaction.Enlist(sc);
try {
    sc.ExecuteNonQuery();
}
catch (SqlException ex){
    transaction.RollBack();
    nhsession.Close();
}
Run Code Online (Sandbox Code Playgroud)

在请求结束时我发出一个CommitTransaction()nhsession.Close()

现在这绝对不会做任何事情:FooClass (fc)尚未刷新/提交到数据库.在Save()该NH所做的操作是高达内存这一点.这意味着nhibernate没有发出sql命令,这意味着SqlCommand (sc)我之后触发的那个命令将失败,因为id不存在.

如果我做了冲洗/间提交Save()SqlCommandFooClass(fc)_cannot_be_rolled_back_,这是一个坏人坏事.目前,为了这个工作我使用一个vanila sql插入 …

c# asp.net nhibernate transactions

6
推荐指数
1
解决办法
3621
查看次数

NHibernate中的全局查找对象引用

是否有可能在NHibernate管理的对象上执行全局反向查找?

具体来说,我有一个名为"Io"的持久化类.跨多个表有大量字段,可能包含该类型的对象.有没有办法(给定一个Io对象的特定实例),检索实际上引用该特定对象的对象列表(任何类型)?(如果能够识别哪些特定字段实际包含引用,则奖励点,但这并不重要.)

由于NHibernate映射定义了所有链接(并且底层数据库具有相应的外键链接),因此应该有一些方法来实现它.

想象一下这种结构:

class Io
{
  public int Id { get; set; }
  // other fields specific to the Io type
}

class ThingOne
{
  public int Id { get; set; }
  public Io SensorInput { get; set; }
  public Io SolenoidOutput { get; set; }
  // other stuff
}

class ThingTwo
{
  public int Id { get; set; }
  public Io SensorInput1 { get; set; }
  public Io SensorInput2 { get; set; }
  public SubThing Doohickey …
Run Code Online (Sandbox Code Playgroud)

c# nhibernate criteria

6
推荐指数
1
解决办法
641
查看次数

NHibernate过滤器集合

使用NHibernate我想过滤一个类中的集合,只包含可能对象的子集.下面我将包括一个示例表数据以帮助解释.我无法使用NHibernate找到这个.

表:数据对象

DataObjectId(PK)/ Name/CurrentVersion

11          "data.txt"      2
12          "info.txt"      3
Run Code Online (Sandbox Code Playgroud)

表:DataObjectVersion

Id/Comment/VersionNumber/DataObjectId(FK)

31   "Genesis"         1          11     <= Ignore this object
32   "Changed data"    2          11     <= Get this object
34   "Genesis"         1          12     <= Ignore this object   
35   "Changed info"    2          12     <= Ignore this object
36   "Added info"      3          12     <= Get this object
Run Code Online (Sandbox Code Playgroud)

我想在一个命令中为每个DataObject加入非外键DataObject.CurrentVersion = DataObjectVersion.VersionNumber.

以下是类和映射文件:

public class DataObject
{
  public virtual int DataObjectId { get; set; }
  public virtual string Name { get; set; }
  public …
Run Code Online (Sandbox Code Playgroud)

versioning nhibernate filter nhibernate-mapping

5
推荐指数
1
解决办法
5804
查看次数

NHibernate mapping with no empty constructor and no setters

I'm testing how difficult it is to use NHibernate with a domain that is completely unaware of it and isn't bent to accodomate any limitations.

On many examples I found on the web the domain being mapped is yet another example of an anemic domain, where objects don't go far from being simple data holders. Sure, this makes the mapping simple and all and it might appeal to data-centric persons/situations, but I don't like hearing the voices in my head …

mapping nhibernate orm nhibernate-mapping

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

是否值得从NHibernate 1.2迁移到NHibernate 2.x?

我们在一个性能不佳的系统中使用nHibernate 1.2.如果我们迁移到最新版本的nHibernate,会有一些性能提升吗?

总体而言,迁移到最新版本的nHibernate是一个好主意吗?

编辑:我想使用以下功能来提高性能.
1.二级缓存.2.加入表.3. MultiQuery批量查询.

migration nhibernate performance batch-file second-level-cache

1
推荐指数
1
解决办法
535
查看次数