在控制器创建实体ARTICLE的操作期间,我遇到了简单验证的问题。我先使用EF 4数据库。实体ARTICLE在实体ACTION(ACTION.ARTICLE_id)中用作外键。这就是代码生成工具向实体ARTICLE添加导航属性的原因,即使它没有多大意义。每次我更新实体时,clasess都会在下面形成(ARTICLE)。我再次检查了所有外键。我真的不知道如何处理这个错误来制作干净的解决方案,而不仅仅是清除控制器动作中的错误。一切 - 甚至视图都是脚手架。
动作:
[HttpPost]
[Authorize(Roles = "ARTICLE_ADMIN")]
public ActionResult Edit(ARTICLE article)
{
if (ModelState.IsValid)
{
article.date_modified = DateTime.Now;
string newimage = this.Request.Form["preview_image_filename"];
string oldimage = this.Request.Form["original_image_filename"];
if (newimage.NotNullOrEmpty())
{
article.preview_image = newimage;
}
else
{
article.preview_image = oldimage;
}
db.Entry(article).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
article.date_modified = DateTime.Now;
ViewBag.ARTICLE_CATEGORY_id = new SelectList(db.ARTICLE_CATEGORY, "ARTICLE_CATEGORY_id", "description", article.ARTICLE_CATEGORY_id);
ViewBag.ARTICLE_STATUS_id = new SelectList(db.ARTICLE_STATUS, "ARTICLE_STATUS_id", "description", article.ARTICLE_STATUS_id);
ViewBag.WEB_USER_id = new SelectList(db.WEB_USER, "WEB_USER_id", "login", article.WEB_USER_id);
return View(article);
}
我正在使用通过代码生成工具生成的实体模型,在元数据类中添加了注释,它不能更简单
public partial class ARTICLE
{
public ARTICLE()
{
this.PROGRAM_WEEK_DAY_ITEM = new HashSet<PROGRAM_WEEK_DAY_ITEM>();
this.STORAGE = new HashSet<STORAGE>();
this.SHOW = new HashSet<SHOW>();
this.ACTION = new HashSet<ACTION>();
}
public int ARTICLE_id { get; set; }
public System.DateTime date_created { get; set; }
public Nullable<System.DateTime> date_modified { get; set; }
public string title { get; set; }
public string html { get; set; }
public int WEB_USER_id { get; set; }
public int ARTICLE_STATUS_id { get; set; }
public int ARTICLE_CATEGORY_id { get; set; }
public Nullable<System.DateTime> date_published { get; set; }
public string preview_image { get; set; }
//code generation tool added those navigation props
public virtual ARTICLE_CATEGORY ARTICLE_CATEGORY { get; set; }
public virtual ARTICLE_STATUS ARTICLE_STATUS { get; set; }
public virtual WEB_USER WEB_USER { get; set; }
public virtual ICollection<PROGRAM_WEEK_DAY_ITEM> PROGRAM_WEEK_DAY_ITEM { get; set; }
public virtual ICollection<STORAGE> STORAGE { get; set; }
public virtual ICollection<SHOW> SHOW { get; set; }
//this one causes trouble I think, but no clue why
public virtual ICollection<ACTION> ACTION { get; set; }
}
元数据类 - 只显示名称和格式:
public class ARTICLE_Metadata
{
[Key]
public int ARTICLE_id { get; set; }
[Display(Name="Vytvořeno")]
public System.DateTime date_created { get; set; }
[Display(Name = "Změněno")]
public Nullable<System.DateTime> date_modified { get; set; }
[Display(Name = "Publikováno")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> date_published { get; set; }
[Display(Name = "Titulek článku")]
public string title { get; set; }
[Display(Name = "Obsah článku")]
[UIHint("tinymce_full"), AllowHtml]
public string html { get; set; }
[Display(Name = "Vytvořil")]
public int WEB_USER_id { get; set; }
[Display(Name = "Status")]
public int ARTICLE_STATUS_id { get; set; }
[Display(Name = "Kategorie")]
public int ARTICLE_CATEGORY_id { get; set; }
[Display(Name = "Náhledový obrázek")]
public string preview_image { get; set; }
}
最后在剃刀视图中形成:
@using (Html.BeginForm("Create", "Articles", FormMethod.Post, new { @class = "base-form" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Nový článek</legend>
@Html.DatePickerFor(model => model.date_published, false)
@Html.HiddenFor(model => model.WEB_USER_id)
<p class="editor-label">
@Html.LabelFor(model => model.ARTICLE_STATUS_id)
@Html.DropDownList("ARTICLE_STATUS_id")
@Html.ValidationMessageFor(model => model.ARTICLE_STATUS_id)
</p>
<p class="editor-label">
@Html.LabelFor(model => model.ARTICLE_CATEGORY_id)
@Html.DropDownList("ARTICLE_CATEGORY_id")
@Html.ValidationMessageFor(model => model.ARTICLE_CATEGORY_id)
</p>
<p class="editor-label">
@Html.LabelFor(model => model.title)
@Html.EditorFor(model => model.title)
@Html.ValidationMessageFor(model => model.title)
</p>
<div class="html-editor">
@Html.EditorFor(model => model.html)
</div>
<p>
<input type="submit" value="Vytvořit" class="submit" />
</p>
</fieldset>
}
当模型验证并进入控制器操作时,ModelState.IsValid == false,ModelState声明属性ACTION的错误,该错误甚至不在表中,并且不应该存在,它是导航属性。
错误有一个例外:从'System.String'类型到'namespace.ACTION'类型的参数转换失败,因为没有类型转换器可以在这些类型之间进行转换。
我试图附加调试器视图图像,但是这个网络不允许它给我。我有其他实体通过控制器管理并以相同的方式查看 - 大约30个不会发生这种情况。
如何在不创建具有相同属性但没有导航属性的额外模型的情况下解决此问题?或者只是阻止此导航属性包含在验证中。或者这是一个新的微软废话功能?
答案 0 :(得分:0)
有时,db-first中的这些奇怪错误来自于实体中导航属性的名称,与另一个实体的名称相同。我自己有时会遇到这些问题,而且我不知道究竟是什么原因。
无论如何,重命名那个导航属性必须让你摆脱那个奇怪的错误......