I have an asp.net-mvc project with the following set up:
class Person {
int PersonId {get; set;}
List<Activity> Activities {get; set;}
}
class Activity {
int ActivityId {get; set;}
int PersonId {get; set;}
[ForeignKey("PersonId")]
Person Person {get; set;}
}
now, when I add a new activity for a person, I send an ajax POST and the action method does the following:
public ActionResult AddActivity(int Id) {
var activity = new Activity();
activity.PersonId = Id;
db.Activity.Add(activity);
db.SaveChanges();
}
is this a bad way of adding an activity because I'm manually adding the foregin key?
is there a way to do this without having to provide the PersonId foreign key and let it be generated automatically (maybe getting the person object, updating its list of activities and then marking it as EntityState.Modified)?
Aucun commentaire:
Enregistrer un commentaire