Problem with fully automatic Nhibernate auto-tuning and guides / UniqueIdentifiers as primary key fields

I am trying to use the functionality of the Fluent-NHibernate car (in the latest software version), and I am encountering problems using Guids as Primary Key fields. If I use integer fields for primary keys, the tables are generated successfully and all the Nhibernate functions work fine. FYI, I use NHibernate to generate my database tables.

Here are a couple of classes with integer identifiers.

using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Reflection; namespace Sample.Data.Entities { public class Employee { public virtual int Id { get; private set; } public virtual string FirstName { get; set; } public virtual string LastName { get; set; } public virtual Store Store { get; set; } } public class Product { public virtual int Id { get; private set; } public virtual string Name { get; set; } public virtual double Price { get; set; } public virtual IList<Store> StoresStockedIn { get; private set; } public Product() { StoresStockedIn = new List<Store>(); } } public class Store { public virtual int Id { get; private set; } public virtual string Name { get; set; } public virtual IList<Product> Products { get; set; } public virtual IList<Employee> Staff { get; set; } public Store() { Products = new List<Product>(); Staff = new List<Employee>(); } public virtual void AddProduct(Product product) { product.StoresStockedIn.Add(this); Products.Add(product); } public virtual void AddEmployee(Employee employee) { employee.Store = this; Staff.Add(employee); } } } 

Here are the same classes with a GUID.

 using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Reflection; namespace Sample.Data.Entities { public class Employee { public virtual Guid Id { get; private set; } public virtual string FirstName { get; set; } public virtual string LastName { get; set; } public virtual Store Store { get; set; } } public class Product { public virtual Guid Id { get; private set; } public virtual string Name { get; set; } public virtual double Price { get; set; } public virtual IList<Store> StoresStockedIn { get; private set; } public Product() { StoresStockedIn = new List<Store>(); } } public class Store { public virtual Guid Id { get; private set; } public virtual string Name { get; set; } public virtual IList<Product> Products { get; set; } public virtual IList<Employee> Staff { get; set; } public Store() { Products = new List<Product>(); Staff = new List<Employee>(); } public virtual void AddProduct(Product product) { product.StoresStockedIn.Add(this); Products.Add(product); } public virtual void AddEmployee(Employee employee) { employee.Store = this; Staff.Add(employee); } } } 

Here is my configuration.

  return Fluently.Configure() .Database(MsSqlConfiguration.MsSql2008 .ConnectionString(c => c.FromConnectionStringWithKey("AAAConnectionString")) .UseReflectionOptimizer() .AdoNetBatchSize(25) .DefaultSchema("dbo") .Cache(c => c .UseQueryCache() .ProviderClass<HashtableCacheProvider>()) .ShowSql()) .Mappings(m=>m.AutoMappings .Add(AutoMap.AssemblyOf<Sample.Data.Entities.Product>() .Where(type => type.Namespace == "Sample.Data.Entities.Product") .Conventions.AddFromAssemblyOf<Sample.Data.Fluent.Conventions.PrimaryKeyNameConvention>() )) .ExposeConfiguration(BuildSchema) .BuildSessionFactory(); 

To get around this problem, I tried to generate conventions (see below) for 1) assigning an identifier field name (although I thought it would be optional) and for 2) creating an Id (which, as I thought, would be automatic). I am not sure what is happening or why this is not working.

 public class PrimaryKeyNameConvention : IIdConvention { public bool Accept(IIdentityInstance id) { return true; } public void Apply(IIdentityInstance id) { id.Column("Id"); } } public class PrimaryKeyGeneratorConvention : IIdConvention { public bool Accept(IIdentityInstance id) { return true; } public void Apply(IIdentityInstance id) { id.GeneratedBy.GuidComb(); } } 

In addition, if I turn off automatic shutdown and use a card with a smooth configuration, the tables will be generated successfully.

It drives me crazy, and I'm sure this is probably a quick fix. Any ideas?

Thanks!

Anthony

+1
source share
1 answer

There seems to be a problem with Fluent Nhibernate version 1.0RC and version 1.0. However, if you download the latest version from the SVN trunk, everything works just fine. It seems that the problem was just a bug in the code that is now fixed.

In addition, I must point out that James Gregory, Paul Batum, and possibly others, are actively working on Fluent NHibernate. The product is developing quite dramatically, and over the past couple of months there have been significant changes in the code.

+4
source

Source: https://habr.com/ru/post/1339689/


All Articles