I have a Comment object with an optional user. On setting the user, I would like to set an additional field with the user's name so that if the user is later deleted the comment will be identifiable to humans.
public class Comment
{
public int Id { get; set; }
public int? CommenterId { get; set; }
public string CommenterName { get; set; }
public string Text { get; set; }
public virtual UserProfile Commenter { get; set; }
}
Commenter and CommenterId are mapped like so with FluentAPI:
HasOptional(t => t.Commenter)
.WithMany()
.HasForeignKey(t => t.CommenterId);
So I'd like to override the setter of Commenter and do something like this:
public virtual UserProfile Commenter{ get; set
{
CommenterName = value.DisplayName;
CommenterId = value.Id
}
}
When I set that up, I see that it is not valid to define a setter without defining a getter. I guess I could define the getter, which would mean looking up the user by Id, but that seems like I'd just be re-implementing something that already works. Is there a correct or better way to do this?