Click here to Skip to main content
15,887,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team

I have a controller and created a View for it. I am using DbContext "eNtsaRegistration and it has table definition "TbTrainingRegForm. I want to save the form fields using List. Now i am getting this exception "
System.InvalidOperationException: 'The model backing the 'eNtsaRegistration' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).'
"

What I have tried:

C#
<pre>  // GET://Home/Save
        public ActionResult SaveRegForm()
        {
            ViewBag.Message = "Details saved successfull";
            return View(db.TrainingRegs.ToList());
        }


// DAL folder
public class eNtsaRegistration:DbContext
{

    public eNtsaRegistration() : base("eNtsaRegistration")
    {
    }  public DbSet<TrainingRegForm> TrainingRegs { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}


// Model
namespace eNtsaRegistrationTraining.Models
{
    public class TrainingRegForm
    {
        [Key]
        public Guid? Id { get; set; }
        public string Title { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Position { get; set; }
        public string Company { get; set; }

        public string StreetAddress { get; set; }

        public string StreetAddressLine { get; set; }

        public string City { get; set; }
        public string StateProvince { get; set; }

        public int ZipCode { get; set; }

        public string Country { get; set; }

        public string Email { get; set; }

        [Required(ErrorMessage = "This field is required")]
        [DataType(DataType.PhoneNumber)]
        public string CellNumber { get; set; }
        public string DietaryRequirement { get; set; }
    }


    public class RegViewAndRoleViewModel
    {
        public DietViewModel DietMain { get; set; }

        public TrainingRegForm RegForm { get; set; }

       public DropDownViewModel ListCountries { get; set; }
        public RegistrationTrainingForm HomeModel { get; set; }
        public RoleViewModel RoleViewModelData { get; set; }

       
    }


@model IEnumerable<eNtsaRegistrationTraining.Models.TrainingRegForm>

@{
    ViewBag.Title = "SaveRegForm";
}

<h2>SaveRegForm</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LastName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Position)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Company)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.StreetAddress)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.StreetAddressLine)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.City)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.StateProvince)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ZipCode)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Country)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Email)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CellNumber)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DietaryRequirement)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Position)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Company)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.StreetAddress)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.StreetAddressLine)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.City)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.StateProvince)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ZipCode)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Country)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CellNumber)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DietaryRequirement)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) 
            
        </td>
    </tr>
}

</table>
Posted
Updated 7-Jul-20 6:21am

Read the error message: it couldn't be much clearer. The DB and the model of the DB are out of step, and it tells you what tool to use to fix that.
 
Share this answer
 
This is an exception thrown by Entity Framework. It means you changed something in your DbContext implementation class and/or one or more of the model classes that define your database tables.

You have two choices to fix this. You can follow the link in the error message and learn about Code First Migrations. You then go back and fix your classes, putting them back to how they were before the exception showed up, setup a migration, then put your changes back in and execute the migration. (You ARE going to need to learn to do this if you ever want to make changes to the production version of your database!)

Or, you can delete the database off the server, then go run your app and let Entity Framework recreate the database. It WILL be empty, of course. All of your data in the database will be lost.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900