nhibernate

Save a Child for a existent Parent

梦想与她 提交于 2019-12-24 08:49:01
问题 I have a model with some inherits and it is using nhibernate to persisti on a Database. The nhibernate mapping with fluent nhibernate is working fine, but I have a scenario where I need to save a child for a existent parent. My model looks like this: public class Item { public long Id { get; set; } public string Name { get; set; } // other properties } public class ItemCommercial : Item { public decimal Value { get; set; } // other properties } In my Database, the respective tables are

NHibernate - updating Identity fields

十年热恋 提交于 2019-12-24 08:32:05
问题 Can we update Id fields in NHibernate like the following? MyClass myObj = MyClass.Retrieve(1); myObj.Id = 999; myObj.Name = "name name"; myObj.Value = 1000; MyClass.Update(myObj); 回答1: You can close the session, execute raw sql to change the primary key, and then open a new session. that is the only thing that works as nhibername relies on primary key identity. you will never ever be able to make nhibernate do it. 回答2: You should never change the identifier (primary key) on an existing object

Skip property on update in NHibernate

。_饼干妹妹 提交于 2019-12-24 08:22:53
问题 Say I have an User entity and it haves a Password property which is not nullable : Map((x) => x.Password).Column("PASSWORD").Not.Nullable(); In the create action, I manually set the Password value as it is a generated hash. It never goes to the View. In the update, I try to save it, but I don't have the Password value. I get this error for Password propery: PropertyValueException: not-null property references a null or transient value This is my Update method: public bool Update(UserViewModel

NHibernate Criteria: howto exclude certain mapped properties/collections?

↘锁芯ラ 提交于 2019-12-24 08:20:04
问题 Here's my (simplified) model: Ticket -> Customer Callback (s) I have my Ticket mapped so that when it's loaded, the Callbacks are as well. base.HasMany<TechSupportCallback>(x => x.Callbacks) .KeyColumn(Fields.TRACKED_ITEM_ID) .Not.LazyLoad() .Inverse() .Cache.ReadWrite(); This is not lazy loading because otherwise I'll get 'no session to load entities' when the web service tries to serialize (and load) the proxy. (Using repositories to fetch data.) It's also bi-directional .. (in the

nHibernate count rows in subquery

不打扰是莪最后的温柔 提交于 2019-12-24 08:05:03
问题 How can I do something like this in nHibernate: select count(*) from (subquery) It is a rather simple query in SQL, but the solution is not so obvious in nHibernate. An obvious solution would be something along the line of: var rowcount = Session.QueryOver<Entity>() .Select(Projections.Alias(Projections.Count(Projections.SubQuery(detachedQuery)), "count")) .FutureValue<int>(); However, this results in an ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than

Nhibernate Linq Sum with calculation causes NotSupportedException

柔情痞子 提交于 2019-12-24 07:49:36
问题 I am using Nhibernate 3.3.3.4001 with SybaseSQLAnywhere12Dialect and trying to use a simple calculation within a sum linq function. I would like to use Linq rather than HQL to achieve this and would be happy to extend Nhibernate with a default linq to hql generator. Using the following Linq query raises the exception System.NotSupportedException: Expression type 'NhSumExpression' is not supported by this SelectClauseVisitor Linq Query var query = (from c in session.Query<Entity>() select c

Lazy loading a portion of a record with NHibernate

眉间皱痕 提交于 2019-12-24 07:40:15
问题 I'm not sure how to explain this. So here goes... I'm trying to fit the method for lazy loading blobs as described here but I'm stuck with only one table. I have a schema (fixed, in a legacy system) which looks something like this: MyTable ID int Name char(50) image byte This is on Informix, and the byte column is a simple large object. Now normally I would query the table with "SELECT ID, Name, (image is not null) as imageexists..." and handle the blob load later. I can construct my object

(Fluent) NHibernate: Map complete table to one object

橙三吉。 提交于 2019-12-24 07:25:26
问题 I have the following database table: Settings Key | Value ---------------|------------------- SomeSetting | Foo AnotherSetting | Bar ... | ... And want to map this to the following object: public class Settings { public virtual string SomeSetting { get; set; } public virtual string AnotherSetting { get; set; } } How could I accomplish this using (Fluent) NHibernate? 回答1: I would map the key/value pairs to a private IDictionary and expose the properties by accessing the dictionary. See Ayende

Nhibernate/hibernate Avoid Insert in joined table or view

為{幸葍}努か 提交于 2019-12-24 07:13:14
问题 I have to join a entity with a view to retrieve some data into properties <join table="XXVIEW" optional="true"> <key column="ID_ENT" /> <property name="Prop1" insert ="false" update ="false" /> <property name="Prop2" insert ="false" update ="false" /> <property name="Prop3" insert ="false" update ="false" /> </join> But when i try to save (insert) it fails becouse it try to insert a record in XXVIEW with ID_Ent I need to have some properties in this entity get from various calculations or

Session Management in Castle Active Record

与世无争的帅哥 提交于 2019-12-24 07:01:06
问题 How do I manage session if I am using Castle Active Record over nHibernate. Basically I can manage the life cycle of ISession on my own if I am using nHibernate directly. But when I am using Castle AR it does not give me a way to manage the life cycle of the session. I want to use single Session per thread. I am using Castle AR in a WCF service and would like to use Session per WCF Request. 回答1: Instead of using ISession, in Castle ActiveRecord you want SessionScope: using(new SessionScope())