naj*_*jam 2 many-to-many scaffolding ef-code-first asp.net-mvc-3
我正在使用EF代码和mvc脚手架开发mvc3.0应用程序.我目前坚持实体之间的多对多关系.我有以下模型.
命名空间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
}
Run Code Online (Sandbox Code Playgroud)
您可以注意到标签和项目之间的多对多关系以及项目和组之间的一对多关系.脚手架我的控制器和视图后,我根据我的模型为我创建了以下数据库.
http://i.stack.imgur.com/v10oO.jpg
除了项目和标签之间的多对多关系外,所有的crud操作都正常工作.检查下面给出的项目创建界面.
http://i.stack.imgur.com/Lzp3j.jpg
我想要的只是一个显示我所有标签的列表框.我必须做出哪些改变.我是否必须更新我的项目模型?或者介绍一个包含所有标记的ProjectViewModel,然后将其传递给我的视图?
您可以在github.com/najamsk/BigApp上找到源代码
谢谢.等待回复.
Scott Hanselman的回答是正确的 - 多对多关系超出了脚手架本地处理的范围(其原因部分在于,有太多不同的常见类型的多对多编辑用户界面,它非常我们不太可能充分猜出你想要的UI类型.
两种选择:
| 归档时间: |
|
| 查看次数: |
2505 次 |
| 最近记录: |