tobynew
tobynew

Reputation: 347

EntityType 'MvcHtmlString' has no key defined - MVC 4

I have a page class that includes a variable of type MvcHtmlString.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using System.Globalization;
using SpringHealthOne.SpringWS;
using System.Configuration;

namespace SpringHealthOne.Models
{
    public class Page 
    {
        public int PageID { get; set; }
        [Required]
        public string Title { get; set; }
        [Required]
        public MvcHtmlString PageBody { get; set; }
        public string MetaTitle { get; set; }
        public string MetaDescription { get; set; }
        public string Keywords { get; set; }
        public bool Published { get; set; }
    }
}

this in itself is fine, and visual studios 2012 reports no errors or warnings. However, when i attempt to start up the project in debug mode it throws:

One or more validation errors were detected during model generation:

\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'MvcHtmlString' has no key defined. Define the key for this EntityType. \tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'MvcHtmlStrings' is based on type 'MvcHtmlString' that has no keys defined.

the line that thows it is in my MenuController:

IEnumerable<MenuItem> menuItems = db.MenuItems.Where(c => c.ParentID == null).OrderBy(c => c.DisplayOrder).Select(c => c);

I've followed it through to the database context which calls the Page class, but i cant work out why im seeing the error specifically.

Context:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.ComponentModel.DataAnnotations.Schema;
using SpringHealthOne.App_Start;
using System.Web.Mvc;

namespace SpringHealthOne.Models
{
    public class SpringerContext : DbContext
    {
        public DbSet<MenuItem> MenuItems { get; set; }
        public DbSet<Page> Pages { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            Database.SetInitializer<SpringerContext>(new SpringerContextInitializer());

            modelBuilder.Configurations.Add(new MenuItemConfiguration());
            base.OnModelCreating(modelBuilder);


        }
    }
}

Im very new to C#, even more so to MVC. Any help would be awesome.

Upvotes: 1

Views: 518

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236268

MvcHtmlString is not primitive type, so you cannot save it directly to field of database. And entity type requires ID property (otherwise you will not be able to set foreign key in database). Use simple string instead of MvcHtmlString if you want to store your Page objects in database.

Also you can mark this property as not mapped and create string property which will be saved to database:

[NotMapped]
public MvcHtmlString PageBody { get; set; }

public string Body
{
    get { return PageBody.ToHtmlString(); }
    set { PageBody = new MvcHtmlString(value); }
}

Upvotes: 4

Related Questions