Question

I have a problem with simple validation during controller Create action for entity ARTICLE. I am using EF 4 database first. Entity ARTICLE is used as foreign key in entity ACTION(ACTION.ARTICLE_id). That's why code generation tool add navigation property to entity ARTICLE, even it does not make not much sense. Each time I update entities the clasess gets to form below(ARTICLE). I checked all foreign key many times again. I really dont know what to do with this error to make clean soluton, not just clearing error in controller action. Everything - even view are scaffolded.

Action:

    [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);
    }

I am using this entity model generated via code generation tool with added annotations in metadata class, it cant be more simple

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; }
}

metadata class - just display names and formats:

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; }
}

and finally form in razor view:

@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>
}

When model validates and comes to controller action, ModelState.IsValid == false, ModelState claims error on property ACTION which is not even present in the table and not supposed to be there, it is navigation property.

Error has an exeption: The parameter conversion from type 'System.String' to type 'namespace.ACTION' failed because no type converter can convert between these types.

I tried to attach debugger view image but this web did not allowed it to me. I have other entities managed via controllers and view the same way - about 30 where this does not happen.

How can I get rid of this problem without creating extra model with the same properties but without navigation ones? Or just prevent this navigation property to be included to validation. Or it is a new microsoft nonsense feature?

Was it helpful?

Solution

Some times these weird errors in db-first comes from that the name of a navigation property in an entity, is same as the name of another entity. I myself experienced these problems sometimes, and I don't know exactly what is the reason.

Anyway, renaming that navigation property must get you rid of that weird error...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top