So I am building a REST service using EF and WebAPI. I have a two classes like this
public class App
{
[Key]
public int ID {get;set;}
public virtual ICollection<User> Users {get;set;}
}
public class User
{
[Key]
public int ID {get;set;}
public virtual ICollection<App> Apps {get;set;}
}
I can see that EF correctly creates the tables and everything is peachy. The problem, is I can't post composed objects through REST, I need to post by ID references and compose them on the server level.
public class UserViewModel
{
[Key]
public int ID {get;set;}
public virtual ICollection<Int> Apps {get;set;}
}
//Automapper config
Mapper.CreateMap<User, UserViewModel>();
I tried doing this with the Automapper, however it is not doing the Object -> ID map on its own. Is there a way to accomplish this without using automapper, or if not, how to do this with automapper?
A good example of the behavior I am expecting is found here-
public class Enrollment
{
public int EnrollmentID { get; set; }
public int CourseID { get; set; }
public int StudentID { get; set; }
public Grade? Grade { get; set; }
public virtual Course Course { get; set; }
public virtual Student Student { get; set; }
}
You can see that Enrollment has a property called CourseID. According to the article "The CourseID property is a foreign key, and the corresponding navigation property is Course." So this maps quite well to a WebAPI REST service, you ignore the Course property and map the CourseID to a viewmodel and POST the enrollment object with the CourseID string. However, how can I translate this behavior to a collection of courses or a many to many relationship?