如何在模型MVC codetemplates定义属性为外键生成它作为FK [英] How define properties as foreign keys in models to MVC Codetemplates generate it as FK
问题描述
我用EF codefirst。大多数人都知道,但以下code为Edit.tt而且还AddView的Create.tt MVC3 T4 codeTemplates的一部分:
< DIV CLASS =主编场>
<#
如果(property.IsForeignKey){
#>
@ Html.DropDownList(&下;#= property.Name#>中,的String.Empty)
<#
}其他{
#>
@ Html.EditorFor(型号=方式>&模型LT;#= property.Name#>)
<#
}
#>
@ Html.ValidationMessageFor(型号=方式>&模型LT;#= property.Name#>)
< / DIV>
正如你所看到如果codeTemplates找到一个属性作为FK自动生成一个DropDownList,但问题是如何定义的道具为FK?这是我的模型:
公共类艺术家{ 众长标识{搞定;组; }
公共字符串名称{;组; }
公共体裁类型{搞定;组; }
}
公共类体裁{
众长标识{搞定;组; }
公共字符串名称{搞定;组; }
公众的ICollection<艺术家>艺术家{搞定;组; }}
公共类MusicGalleryDB:{的DbContext 公共DbSet<艺术家>艺术家{搞定;组; }
公共DbSet<&体裁GT;流派{搞定;组; }}
我所期望的流派艺术家类是FK和CREATE VIEW艺术家,我可以在DropDownList中选择一个类型,但是没有流派之间的任何关系的MVC不产生意见实际上是MVC的视图中的任何的DropDownList艺术家。磨片看看我的数据库表我看到以下内容:
艺术家(ID,姓名,Genre_Id)
类型(ID,标题)
你瞧,艺术家表中的列名Genre_Id这是一个FK体裁表ID与EF codefirst但MVC codeTemplate无法识别FKS自动生成此表。
我怎样才能做到这一点?
我知道我可以写我自己的意见,但我很有趣的MVC在正确的方式自动生成的意见。有谁知道这事吗?
修改
这是我的控制器(MVC通过自动生成):
公共类ArtistController:控制器
{
私人MusicGalleryDB DB =新MusicGalleryDB(); //
// GET:/艺术家/ 公众的ViewResult指数()
{
变种艺术家= db.Artists.Include(一个= GT; a.Genre);
返回查看(artists.ToList());
} //
// GET:/艺术家/详细资料/ 5 公众的ViewResult详细信息(INT ID)
{
艺术家艺术家= db.Artists.Find(ID);
返回查看(艺术家);
} //
// GET:/歌手/制作 公众的ActionResult的Create()
{
ViewBag.GenreID =新的SelectList(db.Genres,ID,标题);
返回查看();
} //
// POST:/歌手/制作 [HttpPost]
公众的ActionResult创建(艺术家艺术家)
{
如果(ModelState.IsValid)
{
db.Artists.Add(艺术家);
db.SaveChanges();
返回RedirectToAction(「指数」);
} ViewBag.GenreID =新的SelectList(db.Genres,ID,标题,artist.GenreID);
返回查看(艺术家);
} //
// GET:/艺术家/编辑/ 5 公众的ActionResult编辑(INT ID)
{
艺术家艺术家= db.Artists.Find(ID);
ViewBag.GenreID =新的SelectList(db.Genres,ID,标题,artist.GenreID);
返回查看(艺术家);
} //
// POST:/艺术家/编辑/ 5 [HttpPost]
公众的ActionResult编辑(艺术家艺术家)
{
如果(ModelState.IsValid)
{
db.Entry(艺术家).STATE = EntityState.Modified;
db.SaveChanges();
返回RedirectToAction(「指数」);
}
ViewBag.GenreID =新的SelectList(db.Genres,ID,标题,artist.GenreID);
返回查看(艺术家);
} //
// GET:/艺术家/删除/ 5 公众的ActionResult删除(INT ID)
{
艺术家艺术家= db.Artists.Find(ID);
返回查看(艺术家);
} //
// POST:/艺术家/删除/ 5 [HttpPost,ActionName(删除)]
公众的ActionResult DeleteConfirmed(INT ID)
{
艺术家艺术家= db.Artists.Find(ID);
db.Artists.Remove(艺术家);
db.SaveChanges();
返回RedirectToAction(「指数」);
} 保护覆盖无效的Dispose(BOOL处置)
{
db.Dispose();
base.Dispose(处置);
}
}
您已经忘记了包括在艺术家$的
GenreID
属性C $ C>表。它更新到下面。
公共类艺术家
{
公众诠释标识{搞定;组; }
公共字符串名称{;组; }
公众诠释GenreId {搞定;组; } //你忘了这
公共体裁类型{搞定;组; }
}
顺便说一句,你不需要使用长
, INT
是标识不够好
。
I use EFCodefirst. Most of you know, but the following code is part of MVC3 T4 CodeTemplates in Edit.tt And also in Create.tt of AddView:
<div class="editor-field">
<#
if (property.IsForeignKey) {
#>
@Html.DropDownList("<#= property.Name #>", String.Empty)
<#
} else {
#>
@Html.EditorFor(model => model.<#= property.Name #>)
<#
}
#>
@Html.ValidationMessageFor(model => model.<#= property.Name #>)
</div>
As you see if the CodeTemplates find a property as a FK generate a DropDownList automatically, but the question is how can define props as a FK? this is my models:
public class Artist {
public long Id { get; set; }
public string Name { get; set; }
public Genre Genre { get; set; }
}
public class Genre {
public long Id { get; set; }
public string Title { get; set; }
public ICollection<Artist> Artists { get; set; }
}
public class MusicGalleryDB : DbContext {
public DbSet<Artist> Artists { get; set; }
public DbSet<Genre> Genres { get; set; }
}
I expected the Genre in Artist class be a FK and in create view of Artist I can select a Genre in dropdownlist, but the mvc don't generate any dropdownlist in views actually as mvc's view there is not any relation between Genre and Artist. whe I look at the Database tables I see the following:
Artist(Id, Name,Genre_Id)
Genre(Id, Title)
as you see there is a column name Genre_Id in Artist table that is a FK to Id of Genre Table This table is generated automatically with EFCodefirst but the MVC CodeTemplate can not recognize FKs.
How can I do this?
I know I can write my own views but I am interesting to mvc generate views automatically in correct way. Does anyone know about this?
Edit:
This is my controller (generated automatically by mvc):
public class ArtistController : Controller
{
private MusicGalleryDB db = new MusicGalleryDB();
//
// GET: /Artist/
public ViewResult Index()
{
var artists = db.Artists.Include(a => a.Genre);
return View(artists.ToList());
}
//
// GET: /Artist/Details/5
public ViewResult Details(int id)
{
Artist artist = db.Artists.Find(id);
return View(artist);
}
//
// GET: /Artist/Create
public ActionResult Create()
{
ViewBag.GenreID = new SelectList(db.Genres, "Id", "Title");
return View();
}
//
// POST: /Artist/Create
[HttpPost]
public ActionResult Create(Artist artist)
{
if (ModelState.IsValid)
{
db.Artists.Add(artist);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.GenreID = new SelectList(db.Genres, "Id", "Title", artist.GenreID);
return View(artist);
}
//
// GET: /Artist/Edit/5
public ActionResult Edit(int id)
{
Artist artist = db.Artists.Find(id);
ViewBag.GenreID = new SelectList(db.Genres, "Id", "Title", artist.GenreID);
return View(artist);
}
//
// POST: /Artist/Edit/5
[HttpPost]
public ActionResult Edit(Artist artist)
{
if (ModelState.IsValid)
{
db.Entry(artist).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.GenreID = new SelectList(db.Genres, "Id", "Title", artist.GenreID);
return View(artist);
}
//
// GET: /Artist/Delete/5
public ActionResult Delete(int id)
{
Artist artist = db.Artists.Find(id);
return View(artist);
}
//
// POST: /Artist/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Artist artist = db.Artists.Find(id);
db.Artists.Remove(artist);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
You have forgotten to include the GenreID
property in the Artist
table. Update it to following.
public class Artist
{
public int Id { get; set; }
public string Name { get; set; }
public int GenreId { get; set; } // You forgot this
public Genre Genre { get; set; }
}
BTW, you dont need to use long
, int
is good enough for Id
.
这篇关于如何在模型MVC codetemplates定义属性为外键生成它作为FK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!