This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Guys, I can't get my Composite key to work, I have the following:
public struct RelationshipId : IRelationshipId
{
[Column("Xxx")]
[ValidatePresence]
private string _Xxx;
[Column("AccountNo")]
[ValidatePresence]
private int _accountNo;
public RelationshipId(string Xxx, int accountNo)
{
_accountNo = accountNo;
_Xxx = Xxx;
}
public int AccountNoId
{
get { return _accountNo; }
set { _accountNo = value; }
}
public string XxxId
{
get { return _Xxx; }
set { _Xxx = value; }
}
public override int GetHashCode()
{
int hashCode = 0;
hashCode = 19 * hashCode + (_accountNo == null ? 0 : _accountNo.GetHashCode());
hashCode = 19 * hashCode + _Xxx.GetHashCode();
return hashCode;
}
}
public partial class Relationship : IRelationship
{
[Transient]
private RelationshipId _relationshipId;
protected override object GeneratedId()
{
return _relationshipId;
}
public static Relationship CreateNew(string Xxx, int accountNo)
{
Relationship relationship = new Relationship();
relationship._relationshipId = new RelationshipId(Xxx, accountNo);
return relationship;
}
public string Xxx
{
get { return _relationshipId.XxxId; }
set { _relationshipId.XxxId = value; }
}
public int AccountNo
{
get { return _relationshipId.AccountNoId; }
set { _relationshipId.AccountNoId = value; }
}
public IRelationshipId ContextId
{
get { return this.Id; }
}
} When I add it to a new UOW and save it throws an account can not equal null exception.... I followed your instruction exactly.... Josh |
|
|
The error you report is "account can not equal null". I am guessing that "account" is "AccountNo" -- is that right? The code you have posted looks a bit odd because the _accountNo field is declared as non-nullable, but in GetHashCode you have a comparison to null, which shouldn't compile. I am therefore guessing that the _accountNo field is actually a nullable int (int?), and that is doesn't contain a value. Change _accountNo to a (non-nullable) int and you should be good to go. I'd also *strongly* encourage you to make _xxx and _accountNo readonly (and remove the associated property setters). Value types, such as RelationshipId, should almost always be immutable, and in the case of LightSpeed composite key types they should *definitely* be immutable (because the ID itself is immutable). |
|