FluentNHibernate community,
It took me a while to figure out the error when trying to run a simple example against a MS SQL 2008 database. But, with a little help from a good friend, I was able to understand the issue. Please allow me to share our findings for future references. At the end, the configuration was correct; the problem was the mapping file. I tried to follow the idea of the “First Project” example posted on the web (http://wiki.fluentnhibernate.org/Getting_started) and the mapping for the store fields is suggested as follows (not including the relationship mapping): public StoreMap() { Id(x => x.Id); Map(x => x.Name); // relation mapping is not included here } The “Id” fields is the primary key and it is an integer field in the database, and it is necessary to define in the mapping how to handle the field, in accordance with DB definitions. In this case, for an MS SQL 2008 database there are two general options, a) Define the field auto-increment (Identity Specification = yes with Identity Increment) and, b) Define as non-auto-increment (Identity Specification = no). For the purpose of the example, I have chosen the later and in the mapping, the “Id” was specified as “Assigned”. In other words the code will pass the value (i.e. the DB will not increment the value). public class StoreMap : ClassMap<Store> { public StoreMap() { Id(x => x.Id).GeneratedBy.Assigned(); Map(x => x.Name); } } This construct corrected the issue found. If the example has more tables, each table mapping must state how the primary key is handled. Armando Pensado On Tue, Jan 4, 2011 at 7:00 PM, Armando Pensado <[email protected]>wrote: > Paul: > > Thank you for your kind response. > > Armando > > > On Sun, Jan 2, 2011 at 1:54 AM, Paul Batum <[email protected]> wrote: > >> I'm not really sure what's gone wrong, but I'll take a guess - maybe your >> dialect isn't being correctly read from your config file. Try specifying it >> via the fluent interface. See the wiki for examples. >> >> You might want to try grabbing the latest code, and building and running >> the Examples.FirstProject. Then change the config to point at your sql 2008 >> database, and change the code in BuildSessionFactory accordingly. If you can >> get that to work, then you should be able to gradually change it to match >> your existing code and see where it breaks. >> >> Paul. >> >> On Wed, Dec 29, 2010 at 12:33 PM, apensado <[email protected]> wrote: >> >>> FluentNHibernate community, >>> >>> I am new the Hibernate and Fluent Hibernate world, and I am trying to >>> explore and understand the benefits of these ideas. However, I am >>> having troubles to make it work with a MS SQL 2008 database, and got >>> to the point that I need help. Hopefully someone in the community can >>> guide me. Here is the issue. >>> >>> I followed the example posted in >>> http://wiki.fluentnhibernate.org/Getting_started >>> but connecting to an MS SQL 2008 database, in a server. I must explain >>> that in the effort to solve the riddle, I have simplified the provided >>> example, reducing all to one table (Store) with Id and Name fields. >>> >>> Running the example, I stubble into an error when the program is >>> trying to commit the transaction. Allow me to present the error I get >>> in the log file (log4net): >>> >>> DEBUG 8 NHibernate.SQL - Batch commands: >>> command 0:UPDATE [Store] SET Name = @p0 WHERE Id = @p1;@p0 = 'Bargin >>> Basin', @p1 = 1 >>> command 1:UPDATE [Store] SET Name = @p0 WHERE Id = @p1;@p0 = >>> 'SuperMart', @p1 = 2 >>> DEBUG 8 NHibernate.AdoNet.AbstractBatcher - Closed IDbCommand, open >>> IDbCommands: 0 >>> ERROR 8 NHibernate.Event.Default.AbstractFlushingEventListener - Could >>> not synchronize database state with session >>> NHibernate.StaleStateException: Batch update returned unexpected row >>> count from update; actual row count: 0; expected: 2 >>> >>> I have set the configuration in a file, using the following construct >>> to load it >>> >>> var cfg = new Configuration(); >>> cfg.Configure(); // read config default style >>> ISessionFactory sessionFactory = >>> Fluently.Configure(cfg) >>> .Mappings(m => >>> m.FluentMappings.AddFromAssemblyOf<Program>() >>> .ExportTo(@"C:\tmp\NHibernateMappings")) >>> .BuildSessionFactory(); >>> >>> The configuration file( hibernate.cfg.xml) is as follows >>> >>> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > >>> <session-factory name="NHibernate.Test"> >>> <property >>> name="connection.driver_class">NHibernate.Driver.SqlClientDriver</ >>> property> >>> <property name="connection.connection_string"> >>> Data Source=IO\IOSQLSERVER;Initial Catalog=TestDb;Integrated >>> Security=True >>> </property> >>> <property name="adonet.batch_size">10</property> >>> <property name="show_sql">true</property> >>> <property >>> name="dialect">NHibernate.Dialect.MsSql2008Dialect</ >>> property> >>> <property name="use_outer_join">true</property> >>> <property name="command_timeout">60</property> >>> <property name="query.substitutions">true 1, false 0, yes >>> 'Y', no >>> 'N'</property> >>> <property >>> >>> name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, >>> NHibernate.ByteCode.Castle</property> >>> </session-factory> >>> </hibernate-configuration> >>> >>> The error suggests that the program was unable to update the database. >>> I have tried different approaches with no success. I have tried using >>> NHibernate.Dialect.MsSql2000Dialect and >>> NHibernate.Dialect.MsSql2008Dialect. I made several variations the >>> configuration unsuccessfully. >>> >>> It occurred to me and I copied the SQL commands indicated in the log >>> file into the SQL Server Management Studio, but the SQL constructs are >>> missing something or are not compatible. Here is the isolated SQL >>> command and error message, >>> >>> UPDATE [Store] >>> SET Name = @p0 WHERE Id = @p1;@p0 = 'Bargin Basin', @p1 = 1 >>> >>> Msg 137, Level 15, State 2, Line 1 >>> Must declare the scalar variable "@p0". >>> >>> I have “googled” for a solution without success, I have the impression >>> that It might be that I am not using the proper assemblies or the >>> configuration could be wrong. At this point I am hoping that someone >>> in the community can give me a hand to go over this hurdle. >>> >>> Thank you before hand for your assitance >>> >>> Armando Pensado >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Fluent NHibernate" group. >>> To post to this group, send email to [email protected]. >>> To unsubscribe from this group, send email to >>> [email protected]<fluent-nhibernate%[email protected]> >>> . >>> For more options, visit this group at >>> http://groups.google.com/group/fluent-nhibernate?hl=en. >>> >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "Fluent NHibernate" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]<fluent-nhibernate%[email protected]> >> . >> For more options, visit this group at >> http://groups.google.com/group/fluent-nhibernate?hl=en. >> > > -- You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.
