1

我正在尝试使用Fluent-NHibernate自动映射功能(在该软件的最新版本),并遇到使用GUID作为主键字段的问题。如果我使用主键的整数字段,表格生成成功,并且所有Nhibernate功能似乎都正常工作。仅供参考,我正在使用NHibernate生成我的数据库表。问题与流畅的Nhibernate自动映射和GUID/UniqueIdentifiers作为主键字段

以下是几个带整数ID的类。

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); 
     } 
    } 
} 

这里是与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); 
     } 
    } 
} 

这是我的配置。

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(); 

要解决这个问题,我试图生成1公约(见下文))命名编号字段(虽然我认为它应该是不必要的)和2)生成的ID(这是我想将会是自动的)。我不确定发生了什么,或者为什么这不起作用。

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(); 
    } 
} 

此外,如果我关闭automapping并使用流利配置的映射表,则会成功生成表。

这使我疯狂,我相信它可能是一个快速修复。有任何想法吗?

谢谢!

安东尼

回答

4

显然,有一个在功能NHibernate版本1.0RC和1.0版本的问题。但是,如果您从SVN主干下载最新版本,那么每件事情都可以完美运行。看来问题只是代码中的一个错误,现在已经被纠正了。另外,我应该注意到,詹姆斯格雷戈里,保罗巴图姆,也许还有其他人,正在积极致力于流利NHibernate。该产品的发展非常迅速,过去几个月中代码发生了重大变化。

+2

感谢您回来并通知,那个问题没有了。因此,我们知道这不是今天任何人的事情。 – Marcel 2010-10-20 14:30:39