0

I am trying to store the details from form to database using entity framework, i am using enum to store dropdown content.while submitting i am facing issue. issue: System.InvalidOperationException: 'The entity type socialwebtable is not part of the model for the current context.'

      [HttpPost]
     public ActionResult register(socialwebtable c1)
    {
        socialwebsiteEntities db = new socialwebsiteEntities();

        //data1 is a table name
        socialwebtable data23 = new socialwebtable();
        data23.name = c1.name;
        data23.bloodgroup = c1.bloodgroup;
        data23.city = c1.city;
        data23.phonenumber = c1.phonenumber;
        db.socialwebtables.Add(data23);
     //   db.socialwebtables.InsertOnSubmit(data23);
        db.SaveChanges();
        return View();
    }

View:

          <div class="form-group">
          @Html.LabelFor(model => model.bloodgroup, htmlAttributes: new { 
          @class = "control-label col-md-2" })
          <div class="col-md-10">
            @Html.EnumDropDownListFor(
             x => x.bloodgroup,
             "Select My Type",
             new { @class = "form-control" })
             @Html.ValidationMessageFor(model => model.bloodgroup, "", new { 
            @class = "text-danger" })
           </div>
           </div>

Model:

              public enum enum1
                 {
             [Display(Name = "O+")]
              O,
             [Display(Name = "A+")]
              B,
             [Display(Name = "B+")]
              A,
             [Display(Name = "AB+")]
              AB,
             [Display(Name = "O-")]
              O1,
             [Display(Name = "A-")]
              B1,
             [Display(Name = "B-")]
              A1,
             [Display(Name = "AB-")]
             AB1,
               }
            public partial class socialwebtable
             {
            public int id { get; set; }
            public string name { get; set; }
            public enum1 bloodgroup { get; set; }
            public string  city  { get; set; }
            public decimal phonenumber { get; set; }
              }
             }
Ashwin Kumar
  • 61
  • 1
  • 11
  • Possible duplicate of [The entity type is not part of the model for the current context](https://stackoverflow.com/questions/20688922/the-entity-type-type-is-not-part-of-the-model-for-the-current-context) – devNull Mar 27 '19 at 19:26

1 Answers1

0

This error occurs if the table(s) are not created at startup. Please place below code in your custom DBContext class to address the issue.

protected void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Estate>().ToTable("socialwebtable");
}

Hope this helps you..

Deepak
  • 81
  • 5
  • public partial class socialwebsiteEntities : DbContext { public socialwebsiteEntities() : base("name=socialwebsiteEntities") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity().ToTable("socialwebtable"); throw new UnintentionalCodeFirstException(); } public virtual DbSet socialwebtables { get; set; } } } – Ashwin Kumar Mar 28 '19 at 15:49