FluentNHibernate教程问题

时间:2013-09-06 12:37:08

标签: c# .net hibernate nhibernate fluent-nhibernate

我一直在尝试完成FluentNHibernate Tutorial,并且在编译时遇到了一些麻烦。我相信我已经在教程中完成了所有内容,并将我的代码与GitHub上的源代码进行了比较,但我在这段代码中不断得到这个异常:

        private static void BuildSchema(Configuration config)
    {
        if (File.Exists(DbFile))
            File.Delete(DbFile);
        new SchemaExport(config)
        .Create(false,true);
    }

来自:

        private static ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
            .Database(
            SQLiteConfiguration.Standard
            .UsingFile("FluentNHTry.db")
            )
            .Mappings( m=> m.FluentMappings.AddFromAssemblyOf<Program>())
            .ExposeConfiguration(BuildSchema)
                .BuildSessionFactory();

        }

例外是:

NHibernate.MappingException: Association references unmapped class: System.Char
   at NHibernate.Cfg.XmlHbmBinding.CollectionBinder.BindCollectionSecondPass(ICollectionPropertiesMapping collectionMapping, Collection model, IDictionary`2 persistentClasses, IDictionary`2 inheritedMetas)
   at NHibernate.Cfg.XmlHbmBinding.CollectionBinder.<>c__DisplayClass13.<AddCollectionSecondPass>b__12(IDictionary`2 persistentClasses)
   at NHibernate.Cfg.Configuration.SecondPassCompile()
   at NHibernate.Cfg.Configuration.GenerateDropSchemaScript(Dialect dialect)
   at NHibernate.Tool.hbm2ddl.SchemaExport.Initialize()
   at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Action`1 scriptAction, Boolean export, Boolean justDrop)
   at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Boolean script, Boolean export, Boolean justDrop)
   at NHibernate.Tool.hbm2ddl.SchemaExport.Create(Boolean script, Boolean export)
   at FluentNHTry.Program.BuildSchema(Configuration config) in c:\Users\JMcKenn1\Documents\Visual Studio Projects\FluentNHTry\FluentNHTry\Program.cs:line 105
   at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration()}

我不知道如何修复它。如果有人可以请我指出我出错的地方,那就太好了。

注意 - 我没有创建数据库,因为我相信代码为我创建了一个,但它并不像我这样认为错误与没有数据库有关。

编辑 - 正如我被问到的,这里是映射和实体类:

映射:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentNHibernate.Mapping;
using FluentNHTry.Entities;

namespace FluentNHTry.Mappings
{
    public class EmployeeMap : ClassMap<Employee>
    {
        public EmployeeMap()
        {
            Id(x => x.Id);
            Map(x => x.FirstName);
            Map(x => x.LastName);
            References(x => x.Store);
        }
    }

    public class ProductMap : ClassMap<Product>
    {
        public ProductMap()
        {
            Id(x => x.Id);
            Map(x => x.Name);
            Map(x => x.Price);
            HasManyToMany(x => x.StoresStockedIn)
                .Cascade.All()
                .Inverse()
                .Table("StoreProduct");
        }
    }

    public class StoreMap : ClassMap<Store>
    {
        public StoreMap()
        {
            Id(x => x.Id);
            Map(x => x.Name);
            HasMany(x => x.Name)
                .Inverse()
                .Cascade.All();
            HasManyToMany(x => x.Products)
                .Cascade.All()
                .Table("StoreProduct");
        }
    }
}

实体:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FluentNHTry.Entities
{
    public class Employee
    {
        public  virtual int Id { get; protected 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; protected set; }
        public virtual string Name { get; set; }
        public virtual double Price { get; set; }
        public virtual IList<Store> StoresStockedIn { get; protected set; }

        public Product()
        {
            StoresStockedIn = new List<Store>();
        }
    }
    public class Store
    {
        public virtual int Id { get; protected 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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我相信你有一个错字:

public class StoreMap : ClassMap<Store>
{
    public StoreMap()
    {
        // ... other properties ...
        HasMany(x => x.Name) // <-- this line is the problem
            .Inverse()
            .Cascade.All();
        // ... other properties ...
    }
}

应该是:

        HasMany(x => x.Staff)

您可以通过错误消息告知这是问题:

  

关联引用未映射的类:System.Char

这说明你告诉NHibernate某个实体有一组char类型的子实体。 char不是实体,因此是问题。 string实现IEnumerable<char>,因此我们可能正在寻找将HasMany应用于字符串属性。

HasMany(x => x.Name)是罪魁祸首。