I am working on a mvc3.0 app using EF code first and mvc scaffolding. I am currently stuck with many to many relation between entities. I have following model.
namespace BigApp.Models {
#region POCO Objects
public class Group
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime UpdatedOn { get; set; }
public virtual ICollection<Project> Projects { get; set; }
}
public class Project
{
public int Id { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime UpdatedOn { get; set; }
public bool isFeatured { get; set; }
public bool isDisabled { get; set; }
public int GroupId { get; set; }
public virtual Group Group { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
public class Tag
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime UpdatedOn { get; set; }
public virtual ICollection<Project> Projects { get; set; }
}
public class Attachment
{
public int Id { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime UpdatedOn { get; set; }
public int ProjectId { get; set; }
public virtual Project Project { get; set; }
}
public class BigAppContext : DbContext
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to drop and regenerate your database
// automatically whenever you change your model schema, add the following
// code to the Application_Start method in your Global.asax file.
// Note: this will destroy and re-create your database with every model change.
//
// System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<BigApp.Models.BigAppContext>());
public DbSet<BigApp.Models.Group> Groups { get; set; }
public DbSet<BigApp.Models.Project> Projects { get; set; }
public DbSet<BigApp.Models.Tag> Tags { get; set; }
public DbSet<BigApp.Models.Attachment> Attachments { get; set; }
}
#endregion
}
You can notice many to many relationship between Tags and projects and one to many relationship between Project and Group. After scaffolding my controllers and views, I have following database created for me based on my model.
https://i.sstatic.net/v10oO.jpg
All crud operation are working fine except many to many relation between project and Tags. Check project create interface given below.
https://i.sstatic.net/Lzp3j.jpg
All I want is a list box showing all my tags. What changes I have to make. Do I have to update my Project Model? or Introduce a ProjectViewModel that holds all the tags and then passed that to my view?
You can find source code at github.com/najamsk/BigApp
Thank you. Waiting for responses.
Scott Hanselman's answer is correct - many-to-many relationships are outside the scope of what scaffolding will handle natively (the reason for this is partly that there are simply too many different common kinds of many-to-many-editing UIs that it's extremely unlikely we can adequately guess what sort of UI you want).
Two options: