无法创建FluentNHibernate.Automapping.AutoMapping Namespace.Class [T]的实例,因为Type.ContainsGenericParameters为true

时间:2011-06-21 01:46:00

标签: c# fluent-nhibernate

这是我得到的错误

CookBook.Tests.CategoryRepository_Fixture.Can_update_category:
FluentNHibernate.Cfg.FluentConfigurationException : An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.


  ----> FluentNHibernate.Cfg.FluentConfigurationException : An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.


  ----> System.ArgumentException : Cannot create an instance of FluentNHibernate.Automapping.AutoMapping`1[CookBook.Repository.Repository`1[T]] because Type.ContainsGenericParameters is true.

这是我的类别对象

public class Category
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

这是我的测试类

[TestFixture]
class CategoryRepository_Fixture
{
    private ISessionFactory _sessionFactory;
    private RecipeConfiguration _configuration;

    private readonly Category[] _categories = new[]
    {
        new Category{Name="Dinner"},
        new Category{Name="Breakfast"},
        new Category{Name="Lunch"},
        new Category{Name="Breakfast"}
    };

    private void CreateInitialData()
    {
        using (ISession session = _sessionFactory.OpenSession())
        using (ITransaction transaction = session.BeginTransaction())
        {
            foreach (var category in _categories)
            { session.Save(category); }
            transaction.Commit();
        }
    }

    [TestFixtureSetUp]
    public void TestFixtureSetUp()
    {
        _configuration = new RecipeConfiguration();
        _sessionFactory = Fluently.Configure()
            .Database(MsSqlCeConfiguration.Standard.ShowSql().ConnectionString("Data Source=CookBook.sdf"))
            .Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Category>(_configuration)))
            .ExposeConfiguration(config => new SchemaExport(config).Execute(false, true, false))
            .BuildSessionFactory();
    }

    [SetUp]
    public void SetupContext()
    {
        CreateInitialData();
    }

    [Test]
    public void Can_add_new_category()
    {
        Category cat = new Category { Name = "Dessert" };
        IRepository<Category> repository = new Repository<Category>();
        repository.Add(cat);

        using (ISession session = _sessionFactory.OpenSession())
        {
            var fromDb = session.Get<Category>(cat.Id);
            Assert.IsNotNull(fromDb);
            Assert.AreNotSame(cat, fromDb);
            Assert.AreEqual(cat.Name, fromDb.Name);
        }
    }

这是存储库类

public class Repository<T> : IRepository<T>
{
    #region IRepository Members

    public void Add(T obj)
    {
        using(ISession session = NHibernateHelper.OpenSession())
        using (ITransaction transaction = session.BeginTransaction())
        {
            session.Save(obj);
            transaction.Commit();
        }
    }
}

我做错了什么?是不是可以使用通用存储库?因为我有大约4个对象都使用相同的存储库。

1 个答案:

答案 0 :(得分:5)

问题是,自动化尝试自动化您的存储库,它不应该映射,只有实体。你的RecipeConfiguration应该告诉FNH要映射哪些类。

public bool ShouldMap(Type type)
{
    return type.In(typeof(Category), typeof(Foo));
}

public bool ShouldMap(Type type)
{
    return type.Namespace == "MyNamespace.Mappings"
}

AutoMap.AssemblyOf<Category>(t => t.Namespace == "MyNamespace.Mappings"))