标签: fluent-nhibernate

Nhibernate Queryover 子查询和 whereexists 有 2 个条件

考虑以下对象结构。

Product
id : int
name : string
attribute : list of Attribute

Attribute
id : int
name: string
value : string
product_id : int
Run Code Online (Sandbox Code Playgroud)

问题是:使用 QueryOver 如何形成子查询以返回具有以下条件的所有产品:

选择同时具有属性的所有产品:

属性名称=“颜色”值=“红色”并且属性名称=“尺寸”值=“XXL”?

编辑:示例sql:

select * from Product p where
exists (select id from attribute where name = 'Color' and value = 'Red' and product_id = p.id)
and
exists (select id from attribute where name = 'Size' and value = 'XXL' and product_id = p.id)
Run Code Online (Sandbox Code Playgroud)

.net fluent-nhibernate queryover

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

如何在 C# 中将默认 FlushMode 更改为 Commit?

所以……已经说过了:

在 C# 中如何更改FlushModeCommit

我的意思是,在 Fluent NHibernate FlushMode 默认设置为 Auto。

所以......要将 FluentMode 设置为Commit,我需要打开会话然后更改它:

var someSessionFactory = ... bla bla ..;
var session = someSessionFactory.OpenSession();
session.FlushMode = FlushMode.Commit;
Run Code Online (Sandbox Code Playgroud)

这会起作用,但是......这意味着我需要在FlushMode.Commit每次打开会话时调用包含的方法。为了初始化 sessionFactory,我有几个包装(意味着只设置一次,然后在打开新上下文时自动使用它),这意味着我不能每次都直接打开会话而不深入研究工厂类型等。

有没有办法将默认 FlushMode从 Auto更改为 Commit?有没有办法做到这一点var sessionFactory = Fluently.Configure(). ...

编辑:

尝试第二件事

public void Initialise(params Assembly[] mappingAssemblies)
{
    this._sessionFactory = Fluently.Configure()
        .Database(
            MsSqlConfiguration.MsSql2008
                .ConnectionString(this._connectionString)
                .AdoNetBatchSize(10)
                .QuerySubstitutions("true 1, false 0, yes 'Y', no 'N'"))
        .Cache(c => c.Not.UseSecondLevelCache().Not.UseQueryCache())
        .Mappings(m =>
        { …
Run Code Online (Sandbox Code Playgroud)

c# nhibernate fluent-nhibernate

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

Nhibernate插入时应在添加事件监听器后更新

我有一个自定义EventListener连接到侦听SaveOrUpdates,现在每当我在我的对象上调用Session.SaveOrUpdate时它会在它应该更新时插入.

这是我的事件监听器:

public class CustomSaveEventListener : DefaultSaveEventListener
{

    protected override object PerformSaveOrUpdate(SaveOrUpdateEvent @event)
    {
        return base.PerformSaveOrUpdate(@event);
    }

}
Run Code Online (Sandbox Code Playgroud)

以下是它的配置方法(流利地说):

 SessionFactory = Fluently.Configure()
              .Database(MsSqlConfiguration.MsSql2005.ConnectionString(c => c.Is(connString)).ShowSql().DoNot.UseReflectionOptimizer)
              .Mappings(m => m.FluentMappings.AddFromAssemblyOf<INC_IncidentMap>())
              .ExposeConfiguration(c => c.EventListeners.SaveOrUpdateEventListeners = new ISaveOrUpdateEventListener[] { new CustomSaveEventListener() })
              .BuildConfiguration().BuildSessionFactory();
Run Code Online (Sandbox Code Playgroud)

我需要事件监听器的原因是因为我需要对持久存在的任何/每个实体执行审计跟踪.

有没有人遇到过这个问题?任何见解将不胜感激.

nhibernate fluent-nhibernate

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

是否有可能将依赖于NHibernate Detached Criteria的测试方法单元化?

我曾尝试使用Moq对使用DetachedCriteria类的存储库上的方法进行单元测试.但是我遇到了一个问题,即我实际上无法模拟内部构建的内部Criteria对象.有没有办法模拟分离标准?

测试方法

        [Test]
        [Category("UnitTest")]
        public void FindByNameSuccessTest()
        {          
            //Mock hibernate here
            var sessionMock = new Mock<ISession>();
            var sessionManager = new Mock<ISessionManager>();
            var queryMock = new Mock<IQuery>();
            var criteria = new Mock<ICriteria>();
            var sessionIMock = new Mock<NHibernate.Engine.ISessionImplementor>();

            var expectedRestriction = new Restriction {Id = 1, Name="Test"};

            //Set up expected returns
            sessionManager.Setup(m => m.OpenSession()).Returns(sessionMock.Object);
            sessionMock.Setup(x => x.GetSessionImplementation()).Returns(sessionIMock.Object);

            queryMock.Setup(x => x.UniqueResult<SopRestriction>()).Returns(expectedRestriction);

            criteria.Setup(x => x.UniqueResult()).Returns(expectedRestriction);

            //Build repository            
            var rep = new TestRepository(sessionManager.Object);

            //Call repostitory here to get list
            var returnR = rep.FindByName("Test");


            Assert.That(returnR.Id == …
Run Code Online (Sandbox Code Playgroud)

c# nhibernate unit-testing fluent-nhibernate detachedcriteria

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

具有3个OR条件的DetachedCriteria

如何使用DetachedCriteria实现此查询:

Select * from 
    MyTable
Where 
    (conditionA = true) or 
    (conditionB = true) or 
    (conditionC = true) or 
    (conditionD = true)
Run Code Online (Sandbox Code Playgroud)

nhibernate fluent-nhibernate

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

关于查询的NHibernate异常

我正在获取执行最基本查询的映射异常.这是我的域类:

public class Project
{
    public virtual string PK { get; set; }
    public virtual string Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

和映射类:

public class ProjectMap :ClassMap<Project>
{
    public ProjectMap()
    {
        Table("PROJECTS");
        Id(x => x.PK, "PK");
        Map(x => x.Id, "ID");
        Map(x => x.Name, "NAME");
        Map(x => x.Description, "DESCRIPTION");
    }
}
Run Code Online (Sandbox Code Playgroud)

组态:

public ISessionFactory SessionFactory
{
    return Fluently.Configure()
        .Database(MsSqlCeConfiguration.Standard.ShowSql().ConnectionString(c => c.Is("data source=" + path)))
        .Mappings(m …
Run Code Online (Sandbox Code Playgroud)

nhibernate fluent-nhibernate

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

流利的nhibernate:删除HasOne关系时出现问题

我有两节课

public  PartMap()
{
   Id(x => x.ID).GeneratedBy.Guid();
   HasOne(x => x.Stock)
          .Cascade.All();
}

public Stock
{
  Id(x => x.ID).GeneratedBy.Guid();
  References(x => x.Part);
}
Run Code Online (Sandbox Code Playgroud)

零件只有一只库存,这就是我使用HasOne的原因。数据已插入好,我有一个零件,一个库存,它们已保存好。我的问题是,当尝试删除时,我收到一个错误,指出零件的库存前期密钥已被违反

"ORA-02292: integrity constraint (PRINERGY.FK121AD9E59966BE23) violated " .
Run Code Online (Sandbox Code Playgroud)

我看到它尝试删除零件而不删除相关库存。我该如何解决?

foreign-keys fluent-nhibernate

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

FluentNHibernate:如何将HasMany(x => x.Addresses).KeyColumn("PersonId")转换为自动化

说我有这些模型:

public class Person
{
    public virtual int Id { get; private set; }
    public virtual ICollection<Address> Addresses { get; private set; }
}

public class Address
{
    public virtual int Id { get; private set; }
    public virtual Person Person { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我希望FluentNHibernate创建以下表:

Person
    PersonId
Address
    AddressId
    PersonId
Run Code Online (Sandbox Code Playgroud)

这可以通过使用流畅的映射轻松实现:

public class PersonMapping : ClassMap<Person>
{
    public PersonMapping()
    {
        Id(x => x.Id).Column("PersonId");
        HasMany(x => x.Addresses).KeyColumn("PersonId");
    }
}

public class AddressMapping : ClassMap<Address>
{
    public AddressMapping()
    {
        Id(x …
Run Code Online (Sandbox Code Playgroud)

nhibernate fluent-nhibernate automapping

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

Nhibernate Flush导致应该没有更新的更新

我正在使用Repo模式,并且已经设置了测试以复制传入的HTTP请求,并在测试完成后立即处理工作单元。

看来,在执行HQL语句之后,再调用displose(依次调用flush)之后,会导致各个元素之间的更新。

非常奇怪-之前有人遇到过吗?

这是我的HQL语句及其执行:

_session.CreateQuery("select distinct t from TaskEntity as t").List<T>()
Run Code Online (Sandbox Code Playgroud)

我将其恢复为最简单的形式-请注意,HQL语句并不直接位于CreateQuery中。

这是我得到的堆栈跟踪:

一世

BM.Data.Informix.IfxParameterCollection.b(Int32 A_0)
IBM.Data.Informix.IfxParameterCollection.GetParameter(Int32 index)
System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item(Int32 index)
NHibernate.Type.Int32Type.Set(IDbCommand rs, Object value, Int32 index)
NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index)
NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, ISessionImplementor session)
NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index)
NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(Object id, …
Run Code Online (Sandbox Code Playgroud)

nhibernate nhibernate-mapping fluent-nhibernate

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

NHibernate - 由指定的guid生成 - 正在覆盖Save()

任何人都可以告诉我为什么即使我有这个映射:

mapping.Id().GeneratedBy.Assigned();
Run Code Online (Sandbox Code Playgroud)

我自己分配了我的实体的ID

打电话给

Session.Save(entity)
Run Code Online (Sandbox Code Playgroud)

导致新Guid与我在实体上分配的Guid不同?

nhibernate fluent-nhibernate

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