【问题标题】:FluentNHibernate Tutorial issueFluentNHibernate 教程问题
【发布时间】:2013-09-10 13:41:44
【问题描述】:

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

【问题讨论】:

  • 请显示您的映射和实体类。

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


【解决方案1】:

我相信你有一个错字:

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&lt;char&gt;,因此我们可能正在寻找将HasMany 应用于字符串属性。

HasMany(x =&gt; x.Name) 是罪魁祸首。

【讨论】:

    猜你喜欢
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 2021-02-15
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多