为什么在使用fluent-nhibernate配置nhibernate时出现MissingMethodException?

时间:2019-03-30 10:00:56

标签: c# nhibernate fluent-nhibernate

在跟随一些教程的同时,我有一个.NET Core 2.2项目,其中nhibernate作为ORM。我已经建立了SessionFactory类,并且在运行应用程序时出现了异常:

FluentNHibernate.Cfg.FluentConfigurationException
  HResult=0x80131500
  Message=An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.


  Source=FluentNHibernate
  StackTrace:
   at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory()
   at Domain.SessionFactory.BuildSessionFactory(String connectionString) in D:\...\Domain\SessionFactory.cs:line 46
   at Domain.SessionFactory.Init(String connectionString) in D:\...\Domain\SessionFactory.cs:line 29
   at Application.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env) in D:\...\.Application\Startup.cs:line 34

Inner Exception 1:
MissingMethodException: Method not found: 'System.Security.PermissionSet System.AppDomain.get_PermissionSet()'.

我尝试安装System.Security.Permissions以及System.AppDomain,但是没有运气。在网上搜寻的三小时内,我也没有找到答案。似乎是在使用AddFromAssemblyAddFromAssemblyOf<Entity>()这两个行中的任何一个(我还不了解它们的用途,但是当它们被注释掉时,我也不例外)。

        private static ISessionFactory BuildSessionFactory(string connectionString)
        {
            var configuration = Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2012.ConnectionString(connectionString)
                .ShowSql())
                .Mappings(m => m.FluentMappings
                    .AddFromAssembly(Assembly.GetExecutingAssembly())
                    //.AddFromAssemblyOf<Entity>()
                    .Conventions.Add(
                        ForeignKey.EndsWith("Id"),
                        ConventionBuilder.Property
                            .When(criteria => criteria.Expect(x => x.Nullable, Is.Not.Set), x => x.Not.Nullable()))
                    .Conventions.Add<TableNameConvention>());

            //configuration.ExposeConfiguration(c =>
            //    new SchemaExport(c).Execute(true, true, false));

            return configuration.BuildSessionFactory();
        }

1 个答案:

答案 0 :(得分:0)

结果出于某种原因,我还需要在应用程序项目中安装nhibernate和fluentnhibernate软件包。