[置顶] 未能加载文件或程序集 NHibernate.XmlSerializers

   在Unity3d中应用mysql的过程中出现这种错误,为解决它花了很多时间。在此发微博加深印象并给同样出现此问题的朋友们提供帮助。


对象类

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

public class TestUser
{
    public virtual int Id { get; set; }
    public virtual string UserName { get; set; }
    public virtual string Passward { get; set; }
    public virtual int Age { get; set; }
}

映射类

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

internal class TestUseMap : ClassMap<TestUser>
{
    public TestUseMap() //x为textUser 的对象
    {
        Id(x => x.Id); //设置id为主键
        Map(x => x.UserName).Column("username");
        Map(x => x.Passward).Column("passward");
        Map(x => x.Age).Column("age");
        Table("testuser");
    }
}
Nhibernate类

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using FluentNHibernate.Automapping;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;

internal class NhibernateHelper
{
    private static ISessionFactory sessionFactory = null;

    private static void InitSessionFactory()
    {
        sessionFactory =
            Fluently.Configure()
                .Database(
                    MySQLConfiguration.Standard.ConnectionString(
                        db => db.Server("localhost").Database("taidou").Username("root").Password("root")))
                .Mappings(x => x.FluentMappings.AddFromAssemblyOf<NhibernateHelper>()).BuildSessionFactory();
    }

    private static ISessionFactory SessionFactory
    {
        get
        {
            if (sessionFactory == null)
            {
                InitSessionFactory();
            }
            return sessionFactory;
        }
    }

    public static ISession OpenSession()
    {
        return SessionFactory.OpenSession();
    }
}
测试类

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

internal class TestUserManager
{
    public IList<TestUser> GetAllUser()
    {
        using (var session = NhibernateHelper.OpenSession())
        {
            var useList = session.QueryOver<TestUser>();
            return useList.List();
        }
    }


    private static void Main(string[] args)
    {
        TestUserManager testUserManager = new TestUserManager();
        IList<TestUser> testUsersList = testUserManager.GetAllUser();

        foreach (TestUser tu in testUsersList)
        {
            Console.WriteLine(tu.UserName);
        }
        Console.ReadKey();
    }
}


报错截图

[置顶] 未能加载文件或程序集 NHibernate.XmlSerializers_第1张图片

出现错误的原因:缺少mysql.data.dll 文件



文件下载地址 

http://pan.baidu.com/s/1gdyMBsR

你可能感兴趣的:(mysql,NHibernate)