Friday, May 6, 2011

Fluent NHibernate Mapping Issue.

Hey everyone.

I am trying to map several tables using Fluent Nhibernate. My tests are giving me the following error: NHibernate.Exceptions.GenericADOException: could not initialize a collection: [FluentWeb.Domain.Employees.Orders#1]

I am trying to map a one to many relationship between Employees and Orders. Orders then has a many to many relation ship with Products (NorthWind). Here are my mappings.. Can someone give me a hand. All was working with HBMs

public class EmployeeMap : ClassMap<Employees>
{

    public EmployeeMap()
    {
        Id(x => x.EmployeeID);
        Map(x => x.FirstName);
        Map(x => x.LastName);
        Map(x => x.City);
        Map(x => x.HireDate);
        Map(x => x.Title);
        HasMany(x => x.Orders)
            .AsBag().WithForeignKeyConstraintName("EmployeeID")
            .Inverse()
            .Cascade.All();


    }
}

 public class OrdersMap : ClassMap<Orders>
{
    public OrdersMap()
    {
        Id(x => x.OrderID);
        Map(x => x.OrderDate);
        Map(x => x.RequiredDate);
        Map(x => x.ShippedDate);
        References(x => x.Employee);
        HasManyToMany(x => x.Products)
            .Cascade.All()
            .WithTableName("Order Details");

    }
}

public class ProductsMap : ClassMap<Products>
{
    public ProductsMap()
    {
        Id(x => x.ProductID);
        Map(x => x.ProductName);
        Map(x => x.UnitPrice);
        HasManyToMany(x => x.Orders)
            .Cascade.All()
            .Inverse()
            .WithTableName("Order Details");


    }
}

Please let me know if more information is needed. Thanks for the help!

-Nick

From stackoverflow
  • Spaces in table names is usually a pretty bad idea. If you can't remove them, try surrounding the table names in backticks, which is NHibernate's database agnostic escaping mechanism.

    .WithTableName("`Order Details`');
    
    Nick : Right... I am using the Norhtwind DB to test. So are you suggesting that the problem is with the "WithTableName()"? I will try this.
    James Gregory : Typically when using plain sql you need to escape table names that contain spaces, so you'll probably need to do the same with NH.

0 comments:

Post a Comment