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 Gr开发者_运维问答oup 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.
http://i.stack.imgur.com/v10oO.jpg
All crud operation are working fine except many to many relation between project and Tags. Check project create interface given below.
http://i.stack.imgur.com/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:
- 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.
- 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.
You may have reached the limits of the "jump start" that Scaffolding provides and may need to write your views on your own. I'll ask Steve Sanderson.
why you do not make a view that do that or you can create a stored procedure in your data base that implement that then you can use in you code in a simple way because the ef has a problem of many to many relationship think out of the box i am sure you are going to get an answer :) mark as answer if it helps
notice : for me i use a custom data access layer not mvc model because i have a lot of custom requirements for my projects
精彩评论