TheBoubou
TheBoubou

Reputation: 19903

How create ISessionFactory with FluentNHibernate

In a project MyProject.Data, I have a folder named mappings with all mapping in.

namespace MyProject.Data.Mappings {
    public class EmployeeMap : ClassMap<Employee>
    {
        public EmployeeMap()
        {
            Id(x => x.Id);
            Map(x => x.FirstName).Not.Nullable().Length(100);
            Map(x => x.LastName).Not.Nullable().Length(100);
        }
    } }

In the MVC project, in the web.config :

  <connectionStrings>
    <add name="db" connectionString="Data Source=localhost\SQLExpress;Initial Catalog=MyDB;Integrated Security=True"/>
  </connectionStrings>

In the global.asax.cs, I'd like create the ISessionFactory :

var nhConfig = Fluently.Configure().Database(MsSqlConfiguration.MsSql2008
   .ConnectionString(c => c.FromConnectionStringWithKey("db")))
   .Mappings(m => m.FluentMappings.AddFromAssemblyOf<?????>())
   .BuildConfiguration();
ISessionFactory SessionFactory = nhConfig.BuildSessionFactory();

I need a : .Mappings(m => m.FluentMappings.AddFromAssemblyOf<?????>()) for each mapping file ?

Thanks,

Upvotes: 1

Views: 1400

Answers (1)

Newbie
Newbie

Reputation: 7249

No, that serves that purpose of telling FNH which assembly to look for all your mappings. Just specify any one and you are good to go.

So, the code below will work for you if all your mappings live in the same assembly.

var nhConfig = Fluently.Configure().Database(MsSqlConfiguration.MsSql2008
.ConnectionString(c => c.FromConnectionStringWithKey("db")))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<EmployeeMap>())
.BuildConfiguration();

Upvotes: 1

Related Questions