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 dou开发者_如何学JAVAble 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
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
.
精彩评论