PersistenceSpecification
Hi There,
I have a relationship of:
public class AccountMapping
{
public AccountMapping()
{
// Fields:
Map(x => x.Name).Length(50);
// Relationships:
HasMany(x => x.Users).Cascade.All();
}
}
public class UserMapping
{
public UserMapping()
{
// Fields:
Map(x => x.Login).Length(50).Unique();
// Relationships:
References(x => x.Account).Cascade.All().Not.Nullable();
}
}
Im trying to test an account is created correctly using the PersistenceSpecification class.
[Test]
public void Can_Correctly_Map_Account()
{
var account = DataHelper.GenerateFakeAccount();
new PersistenceSpecification<Account>(Session)
.CheckProperty(c => c.Name, "Name")
.CheckList(c => c.Users, new List<User>() { new User { Login = "LoginPerson" } } )
.VerifyTheMappings();
}
I end up in a chicken and the egg situation, when the persistance spec saves the account I need an account for the user, but one hasn't been created, Is there a way to handle this situation?
Thanks!
Maxus
Support Staff 2 Posted by Paul Batum on 24 Oct, 2010 03:35 AM
It sort of depends on how you have your domain objects maintaining the
relationship. Most people will use Account.AddUser(User u) and
Account.RemoveUser(User u) methods and allow those methods to maintain the
bidirectional relationship. There's a good way to use the persistence spec
to check these:
http://groups.google.com/group/fluent-nhibernate/browse_thread/thread/cebc70ff873e4fd2/
On Sat, Oct 23, 2010 at 4:20 PM, Maxus <
***@tenderapp.com<tender%***@tenderapp.com>
> wrote:
3 Posted by Maxus on 24 Oct, 2010 07:16 AM
Hi Paul,
Yes that is eaxctly what I'm looking for, but I'm still getting:
NHibernate.PropertyValueException : not-null property references a null or transient value User.Account
the add method on the account is:
In the PersistenceSpecification i have added this:
.CheckList(c => c.Users, users, (a, u) => a.AddUser(u))
The account entity is simple:
The user entity:
I think im just missing something simple, any ideas?
Thanks,
M
4 Posted by Maxus on 24 Oct, 2010 11:57 AM
Seem to have got it working, not exactly sure how, if i figure it out will post the details.
Thank you for your help Paul.
-M