用流畅的Nhibernate Automapping和Guids / UniqueIdentifiers作为主键字段 [英] Issue With Fluent Nhibernate Automapping and Guids / UniqueIdentifiers as Primary Key Fields

查看:157
本文介绍了用流畅的Nhibernate Automapping和Guids / UniqueIdentifiers作为主键字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Fluent-NHibernate自动映射功能(在最新版本的软件中),并且遇到了使用Guids作为主键字段的问题。如果我使用主键的整数字段,表成功生成,所有Nhibernate功能似乎正常工作。我用NHibernate生成我的数据库表。



以下是一些带有整数ID的类。

 使用System; 
使用System.Collections;
using System.Collections.Generic;
使用System.ComponentModel;
使用System.ComponentModel.DataAnnotations;
使用System.Reflection;

命名空间Sample.Data.Entities
{
public class Employee
{
public virtual int Id {get;私人设置}
公共虚拟字符串名字{get;组; }
public virtual string LastName {get;组; }
public virtual Store Store {get;组; }
}

public class Product
{
public virtual int Id {get;私人设置}
public virtual string Name {get;组; }
public virtual double Price {get;组; }
公共虚拟IList< Store> StoresStockedIn {get;私人设置}

public Product()
{
StoresStockedIn = new List< Store>();



public class Store
{
public virtual int Id {get;私人设置}
public virtual string Name {get;组; }
公共虚拟IList<产品>产品{get;组; }
公共虚拟IList<员工>工作人员{get;组; }
$ b $ 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; 
使用System.Collections;
using System.Collections.Generic;
使用System.ComponentModel;
使用System.ComponentModel.DataAnnotations;
使用System.Reflection;

命名空间Sample.Data.Entities
{
public class Employee
{
public virtual Guid Id {get;私人设置}
公共虚拟字符串名字{get;组; }
public virtual string LastName {get;组; }
public virtual Store Store {get;组; }
}

public class Product
{
public virtual Guid Id {get;私人设置}
public virtual string Name {get;组; }
public virtual double Price {get;组; }
公共虚拟IList< Store> StoresStockedIn {get;私人设置}

public Product()
{
StoresStockedIn = new List< Store>();



public class Store
{
public virtual Guid Id {get;私人设置}
public virtual string Name {get;组; }
公共虚拟IList<产品>产品{get;组; }
公共虚拟IList<员工>工作人员{get;组; }
$ b $ 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);





$ b

这是我的配置。 / b>

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

为了解决这个问题,我试图生成约定(见下面)字段(虽然我认为它应该是没有必要的)和2)生成Id(我认为这是自动的)。

  public class PrimaryKeyNameConvention:IIdConvention 
{
public bool接受(IIdentityInstance id)
{
return true;

public void Apply(IIdentityInstance id)
{
id.Column(Id);


$ b public class PrimaryKeyGeneratorConvention:IIdConvention
{
public bool Accept(IIdentityInstance id)
{
return true ;

public void Apply(IIdentityInstance id)
{
id.GeneratedBy.GuidComb();






$ b

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

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

谢谢!
$ b $安东尼

解决方案

显然,在Fluent Nhibernate版本1.0RC和版本1.0中存在一个问题。但是,如果你从SVN主干下载最新版本,那么每件事情都是完美的。看来这个问题只是代码中的一个bug,现在已经被纠正了。

另外,我应该注意到,James Gregory,Paul Batum和其他人积极致力于流利的NHibernate。该产品发展迅猛,在过去几个月里,代码已经发生了重大变化。


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

Here are a couple of classes with integer IDs.

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 GUIDs.

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 work around the issue, I attempted to generate conventions (see below) for 1) naming the Id field (although I thought it should have been unnecessary) and for 2) generating the Id (which I thought would have been automatic). I am unsure 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();
    }
}

Also, if I turn automapping off and use Fluently configured map the tables are generated successfully.

This is driving me nuts, and I am sure it is probably a quick fix. Any ideas?

Thank you!

Anthony

解决方案

Apparently, there was an issue in the Fluent Nhibernate version 1.0RC and version 1.0. However, if you download the latest version from the SVN trunk, every thing works perfectly. It appears that the problem was simply a bug in the code which has now been corrected.

Also, I should note that James Gregory, Paul Batum, and perhaps others, are actively working on Fluent NHibernate. The product is evolving pretty dramatically and there have been significant changes to the code over the last couple of months.

这篇关于用流畅的Nhibernate Automapping和Guids / UniqueIdentifiers作为主键字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆