0

我一直在尝试完成FluentNHibernate 教程,但在编译时遇到了一些麻烦。我相信我已经按照教程中的说明完成了所有操作,并将我的代码与 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);
        }
    }
}
4

1 回答 1

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 一个实体有一个类型为 的子实体的集合charchar不是实体,因此存在问题。 stringimplements IEnumerable<char>,所以我们可能正在寻找HasMany应用于字符串属性的方法。

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

于 2013-09-10T14:36:56.340 回答