I'm trying to do an insert into a database. I have the following two entities:
USER:
public class User
{
private UserDescription _userDescription;
public virtual uint Id { get; set; }
public virtual string Email { get; set; }
public virtual string Username { get; set; }
public virtual UserDescription UserDescription
{
get { return _userDescription; }
set { _userDescription = value; }
}
public virtual void Add(UserDescription userDescription)
{
if (_userDescription != null)
{
_userDescription.User = null;
}
_userDescription = userDescription;
_userDescription.User = this;
}
public class UserMap : ClassMap<User>
{
public UserMap()
{
Table("users");
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.Email).Not.Nullable();
Map(x => x.Username).Not.Nullable();
HasOne(x => x.UserDescription).Cascade.All().LazyLoad();
}
}
}
And USERDESCRIPTION:
public class UserDescription
{
public virtual uint Id { get; set; }
public virtual User User { get; set; }
public virtual uint UserId { get; set; }
public virtual string Firstname { get; set; }
public virtual string Lastname { get; set; }
public class UserDescrptionMap : ClassMap<UserDescription>
{
public UserDescrptionMap()
{
Table("usersdescription");
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.UserId);
Map(x => x.Firstname);
Map(x => x.Lastname);
HasOne(x => x.User).Constrained().ForeignKey();
}
}
}
And my JSON that I pass through looks like the following:
{
"Email": "[email protected]",
"Username": "Something123",
"UserDescription": {
"Firstname": "John",
"Lastname": "Doe"
}
}
I tried doing an insert into User hoping that it would cascade down and also insert into the child table UserDescription, but that didn't work. So instead I insert into user first then insert into UserDescription (not sure if this is ideal).
Anyway, I get the following error when I try to insert into UserDescription after I successfully insert to User:
not-null property references a null or transient value Users.Entities.UserDescription.User
Any suggestions?