Fluent NHibernate PersistenceSpecification
I got the following error message:
System.IndexOutOfRangeException: Invalid index 9 for this SqlParameterCollection with Count=9..
And I absolutely wonder why oO?!
The database schema and the hbm.xml files were all correctly created with FluentNHibernate. The error ocurrs in the CanCorrectlyMapBookmethod, the PersistenceSpecificationTest runs without an error.
[TestClass]
public class PersistenceSpecificationTests
{
private static ISession _session;
[TestInitialize]
public void PersistenceSpecificationTest()
{
_session = Helper.CreateSessionFactory(false, false).OpenSession();
}
[TestMethod]
public void CanCorrectlyMapBook()
{
new PersistenceSpecification<Book>(_session)
.CheckProperty(p => p.IncludesCDDVD, true)
.CheckProperty(p => p.Isbn, "1232324983sfdsdkfj")
.CheckProperty(p => p.Name, "My Book")
.VerifyTheMappings();
}
}
The number 9 might come from the amount of columns the book has got. At first I thought I have to check all properties, but I tested this with another enity in another project and it worked correctly.
Anyone has an idea?
edit:
Here my domain object + mapping:
public interface IEntity
{
int Id { get; set; }
}
public abstract class LoanedItem : IEntity
{
public virtual int Id { get; set; }
public virtual DateTime DateOfIssue { get; set; }
public virtual bool IsLoaned { get; set; }
public virtual String Name { get; set; }
public virtual Employee Lo开发者_如何学CanedBy { get; set; }
public virtual Release Release { get; set; }
public virtual Publisher Publisher { get; set; }
public virtual bool IncludesCDDVD { get; set; }
}
public class Book : LoanedItem
{
public virtual string Isbn { get; set; }
public virtual int Author { get; set; }
}
public class BookMap : ClassMap<Book>
{
public BookMap()
{
// identity mapping
Id(p => p.Id).Column("BookID");
// column mapping
Map(p => p.Author);
Map(p => p.Isbn);
Map(p => p.IncludesCDDVD);
Map(p => p.IsLoaned);
Map(p => p.Name);
// component mapping
// Publisher
Component(p => p.Publisher, m =>
{
m.Map(x => x.Name);
m.Map(x => x.Homepage);
});
// Release
Component(p => p.Release, m =>
{
m.Map(x => x.ReleaseDate);
m.Map(x => x.ReleaseNumber);
});
// reference/association
References(p => p.LoanedBy).Column("EmployeeID");
}
EDIT:
Ok the problem above could be solved.
BUT how can I check a component? When I check a component with CheckProperty an error occurs ... "expected 'DomainModel.Model.Book' but get 'DomainModel.Model.Book' ... ugh :) well whats wrong there? it is exactly the same domain object. I created a new question
The problem probably are Name properties. You have 2 properties named "Name" - one in Book, and one in Publisher component. AFAIR FluentNHibernate will map BOTH of those properties to column "NAME" (check with generated hbms), which would result in an error you're getting.
Try to specify different column name for one of those properties (preferable in Publisher component - add a prefix or so) and see whether this helps.
I just tried your code in a new project, simplifying the POCOs and mappings to only what you're testing in your test class. This is what I have:
public abstract class LoanedItem
{
public virtual int Id { get; set; }
public virtual String Name { get; set; }
public virtual bool IncludesCDDVD { get; set; }
}
public class Book : LoanedItem
{
public virtual string Isbn { get; set; }
}
public class BookMap : ClassMap<Book>
{
public BookMap()
{
// identity mapping
Id(p => p.Id).Column("BookID");
// column mapping
Map(p => p.Isbn);
Map(p => p.IncludesCDDVD);
Map(p => p.Name);
}
And the test runner:
[TestClass]
public class PersistenceSpecificationTests
{
private static ISession _session;
[TestInitialize]
public void PersistenceSpecificationTest()
{
var cfg = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory().UseReflectionOptimizer())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Book>())
.BuildConfiguration();
_session = cfg.BuildSessionFactory().OpenSession();
new SchemaExport(cfg).Execute(false, true, false, _session.Connection, null);
}
[TestMethod]
public void CanCorrectlyMapBook()
{
new PersistenceSpecification<Book>(_session)
.CheckProperty(p => p.IncludesCDDVD, true)
.CheckProperty(p => p.Isbn, "1232324983sfdsdkfj")
.CheckProperty(p => p.Name, "My Book")
.VerifyTheMappings();
}
}
It's using an in-memory SQLite database. The test passes fine, so the problem must be elsewhere.
精彩评论