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

发布于 2024-11-16 17:54:08 字数 3172 浏览 5 评论 0原文

这是我收到的错误,

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 个对象,它们都使用同一个存储库。

Here is the error i'm getting

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.

here is my Category object

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

here is my test class

[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);
        }
    }

Here is the repository class

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();
        }
    }
}

What am i doing wrong? is it not possible to use a generic repository? because I have about 4 objects that all use the same repository.

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

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

发布评论

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

评论(1

完美的未来在梦里 2024-11-23 17:54:08

问题是,自动映射尝试自动映射您的存储库,它不应该映射,而只能映射实体。您的 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"))

the problem is, that automapping tries to automap your repository, that it shouldn't map, only entities. your RecipeConfiguration should tell FNH which classes to map.

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

or

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

or

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