Reputation: 30208
I have 3 classes:
public class User
{
...
public List<SerialHistory> SerialHistory { get; set;}
...
}
public class SerialHistory
{
...
public DateTime DateAdded { get; set; }
public SerialNumber SerialNumber { get; set; }
...
}
public class SerialNumber
{
...
public User User { get; set; }
...
}
I would really like it so that when I do this:
user.SerialHistory.Add(new SerialHistory {
SerialNumber = serialNumber,
DateAdded = DateTime.Now
});
that serialNumber.User
gets user
automatically assigned to it.
Is this possible or is this beyond EntityFramework's capabilities?
Upvotes: 0
Views: 78
Reputation: 177133
No, there is no special option in Entity Framework to ensure this. User.SerialHistory
and SerialNumber.User
are navigation properties of two different relationships. What you have is a special business logic that cannot be enforced by a foreign key relationship in the database, hence it cannot be enforced by any mapping (that basically just represents foreign key relationships) with Entity Framework.
Your best option is maybe a special method of User
:
public void AddSerialNumber(SerialNumber serialNumber)
{
serialNumber.User = this;
SerialHistory.Add(new SerialHistory {
SerialNumber = serialNumber,
DateAdded = DateTime.Now
});
}
Upvotes: 1