مشكلة مع Automabing Nhibernate بطلاقة و GUIDS / فريدة من نوعها كحقول رئيسية أساسية

StackOverflow https://stackoverflow.com/questions/1360157

سؤال

أحاول استخدام وظيفة Automabing Flugent Nhibernate (في أحدث إصدار من البرنامج) وأنا أواجه مشكلات باستخدام GUIDS كحقول رئيسية أساسية. إذا استخدمت حقول عدد صحيح للمفاتيح الأساسية ، يتم إنشاء الجداول بنجاح ويبدو أن جميع وظائف nhibernate تعمل بشكل جيد. لمعلوماتك ، أنا أستخدم nhibernate لإنشاء جداول قاعدة البيانات الخاصة بي.

فيما يلي بعض الفصول مع معرفات عدد صحيح.

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

فيما يلي نفس الطبقات مع 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);
        }
    }
}

هنا تكوين بلدي.

    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) تسمية حقل المعرف (على الرغم من أنني اعتقدت أنه كان ينبغي أن يكون غير ضروري) وللتوليد المعرّف (الذي اعتقدت أنه سيكون تلقائيًا). أنا غير متأكد مما يحدث أو لماذا لا يعمل هذا.

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

أيضًا ، إذا قمت بإيقاف تشغيل السيارات واستخدمت الخريطة التي تم تكوينها بطلاقة ، يتم إنشاء الجداول بنجاح.

هذا يقودني إلى الجوز ، وأنا متأكد من أنه على الأرجح حلًا سريعًا. أيه أفكار؟

شكرًا لك!

أنتوني

هل كانت مفيدة؟

المحلول

على ما يبدو ، كانت هناك مشكلة في إصدار Nhibernate بطلاقة 1.0RC والإصدار 1.0. ومع ذلك ، إذا قمت بتنزيل أحدث إصدار من صندوق SVN ، فكل شيء يعمل بشكل مثالي. يبدو أن المشكلة كانت مجرد خطأ في الكود الذي تم تصحيحه الآن.

أيضا ، أود أن أشير إلى أن جيمس غريغوري ، بول باتوم ، وربما آخرين ، يعملون بنشاط على Nhibernate بطلاقة. يتطور المنتج بشكل كبير وكان هناك تغييرات كبيرة على الكود خلال الشهرين الماضيين.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top