我一直在尝试完成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);
}
}
}