Issue With Fluent Nhibernate Automapping and Guids / UniqueIdentifiers as Primary Key Fields

爱⌒轻易说出口 提交于 2019-12-11 10:19:48

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/1360157/issue-with-fluent-nhibernate-automapping-and-guids-uniqueidentifiers-as-primar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!