Search code examples
asp.net-mvc-3many-to-manyef-code-firstscaffolding

stuck with asp.net mvc 3.0 Scaffolding in case of many to many relationship


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.


Solution

  • 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:

    1. Use scaffolding as a starting point and, from what you've already got, add a multiselect list onto your "Edit Project" screen so users can add/remove tags. This is a manual development step and will require more familiarity with ASP.NET MVC development.
    2. Alternatively, you can create an explicit "link entity", e.g. called TagAssignment, that has a one-to-many relationship with both Project and Tag (so a Project has many TagAssignments, and a Tag has many TagAssignments). Then scaffolding will be able to generate for you a UI for creating arbitrary numbers of TagAssignments between projects and tags.