问题描述
我正在使用EF code第一和MVC脚手架mvc3.0应用程序。目前我坚持到许多实体之间的关系很多。我有以下模式。
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.
命名空间BigApp.Models
{
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
}
您可以看到很多对项目与群体之间的多对多关系标签和项目和1之间一对多的关系。脚手架我的控制器和意见后,我有以下根据我的模型,我创建的数据库。
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.
所有CRUD操作工作正常,除了许多项目和标签之间的许多关系。检查项目中创建如下界面。
All crud operation are working fine except many to many relation between project and Tags. Check project create interface given below.
我要的是显示我的所有标签的列表框。我有什么样的变化作出。我一定要更新我的项目模型?或引入保存所有标签,然后通过了我的看法?一个ProjectViewModel
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?
您可以找到源头code。在
github.com/najamsk/BigApp
You can find source code at github.com/najamsk/BigApp
感谢您。等待响应。
推荐答案
斯科特Hanselman的答案是正确的 - 多到许多关系是什么脚手架将在本地处理范围外(这样做的原因是部分有简单太多不同的常见种类的多对许多编辑的用户界面,它是极不可能的,我们可以充分地猜测你想要什么样的用户界面)。
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).
两个选项:
- 使用脚手架为起点,并从你已经有了,添加多选名单到您的编辑专案的画面,使用户可以添加/删除标记。这是一个手工的发展步骤,并需要与ASP.NET MVC的发展更深入的了解。
- 另外,你可以创建一个明确的链接的实体,如所谓TagAssignment,具有与两个项目和标签一个一对多的关系(因此,对项目有很多TagAssignments和标签有很多TagAssignments)。这时脚手架就能为您生成创建项目和标签之间TagAssignments任意数量的UI。
这篇关于坚持用asp.net mvc的3.0脚手架在很多的情况下,一对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!