Fluent NHibernate 没有实体类的持久化

发布于 2024-11-19 15:25:27 字数 2569 浏览 2 评论 0原文

我用谷歌搜索了一下,似乎找不到解决我的问题的方法。 我试图在这里获取演示项目: http://wiki. Fluentnhibernate.org/Getting_started 使用 sql-server 2008。

我似乎无法正确创建 sessionFactory。

代码:

    private static ISessionFactory CreateSessionFactory()
    {
        try
        {   
            return Fluently.Configure()
                           .Database(MsSqlConfiguration.MsSql2008
                                .ConnectionString(c => c
                                    .Server("localhost")
                                    .Database("gisli")
                                    .TrustedConnection()).ShowSql())
                            .Mappings(m => { 
                                m.HbmMappings.AddFromAssemblyOf<Employee>(); 
                                m.HbmMappings.AddFromAssemblyOf<Store>();
                                m.HbmMappings.AddFromAssemblyOf<Product>();

                            })
                   .BuildSessionFactory();


        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            if(e.InnerException !=null)
                Console.WriteLine(e.InnerException.Message);
                return null;
        }
    }

我也尝试过使用自动映射,但未能使其工作。

我的所有实体类都是公共的。

实体类和映射类的示例:

public class Product
{
    public virtual int Id { get; private set; }
    public virtual string Name { get; set; }
    public virtual double Price { get; set; }
    public virtual IList<Store> StoresStockedIn { get; set; }

    public Product()
    {
        StoresStockedIn = new List<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");
    }
}

编辑:

我也尝试过这个:

    private static ISessionFactory CreateSessionFactory()
    {
        return Fluently.Configure()
          .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c =>             c.Server(".").Database("gisli").TrustedConnection()))
          .Mappings(m =>
            m.FluentMappings.AddFromAssemblyOf<Program>())
          .BuildSessionFactory();
    }

编辑: 第二个例子实际上就是解决方案。我一开始不小心将项目命名为 FluentNHibernate,当我重命名该项目时,它有点混淆了。 谁能看出我哪里搞砸了?

真挚地 吉斯利

I've Googled my ass off and can't seem to find the solution to my problem.
I trying to get the demo project here: http://wiki.fluentnhibernate.org/Getting_started
to work with sql-server 2008.

I can't seem to create the sessionFactory correctly.

The code:

    private static ISessionFactory CreateSessionFactory()
    {
        try
        {   
            return Fluently.Configure()
                           .Database(MsSqlConfiguration.MsSql2008
                                .ConnectionString(c => c
                                    .Server("localhost")
                                    .Database("gisli")
                                    .TrustedConnection()).ShowSql())
                            .Mappings(m => { 
                                m.HbmMappings.AddFromAssemblyOf<Employee>(); 
                                m.HbmMappings.AddFromAssemblyOf<Store>();
                                m.HbmMappings.AddFromAssemblyOf<Product>();

                            })
                   .BuildSessionFactory();


        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            if(e.InnerException !=null)
                Console.WriteLine(e.InnerException.Message);
                return null;
        }
    }

I have also tried to use the automapping and have not been able to make it work.

All of my entity classes are public.

Example of entity class and mapping class:

public class Product
{
    public virtual int Id { get; private set; }
    public virtual string Name { get; set; }
    public virtual double Price { get; set; }
    public virtual IList<Store> StoresStockedIn { get; set; }

    public Product()
    {
        StoresStockedIn = new List<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");
    }
}

EDIT:

I also tried this:

    private static ISessionFactory CreateSessionFactory()
    {
        return Fluently.Configure()
          .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c =>             c.Server(".").Database("gisli").TrustedConnection()))
          .Mappings(m =>
            m.FluentMappings.AddFromAssemblyOf<Program>())
          .BuildSessionFactory();
    }

EDIT:
The second example is in fact the solution. I had accidentally named the project FluentNHibernate in the beginning and when I renamed the project it some how got mixed up.
Can anyone see where I´m messing it up?

sincerely
Gísli

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

涙—继续流 2024-11-26 15:25:27

您的第一个示例实际上不包含任何流畅的映射,仅包含 HBM 映射。

只要您的映射与 Program 位于同一程序集中,您的第二个示例就应该可以工作。

Your first example doesn't actually include any fluent mappings, only HBM mappings.

Your second example should work, as long as your mappings are in the same assembly as Program.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文