简体   繁体   中英

How to use more than one model in your DbContext using asp.net mvc?

I have a data access layer (class and has table definition). Each time I want to save it using view. It goes and stores all the objects to the table definition.

The problem and challenge I am having: I want to figure it out when using more than 3 models point to DbContext , to save the field form? Anyone who has done this similar task before if so kindly please share your ideas around. What am I missing? The form its not saving the field it sees (TbTrainingRegForm) table definition has been changed.

Models:

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

Get function:

// GET:TrainingRegForm/Create/WebRequest.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SubmitRegDetails([Bind(Include= "Id, Title, FirstName, LastName, Position, Company, StreetAddress, StreetAddressLine, City, StateProvince, ZipCode,Email, CellNumber, DietaryRequirement")]TrainingRegForm eNtsaTraining)
{
    if(ModelState.IsValid)
    {
        eNtsaTraining.Id = Guid.NewGuid();
        db.TrainingRegs.Add(eNtsaTraining);
        db.SaveChanges();
        return RedirectToAction("SaveRegForm");
    }

    // Validates when empty.
    if(ModelState.IsValid)
    {
        return RedirectToAction("SaveRegForm");
    }

    return View(eNtsaTraining);
}

View:

@using eNtsaRegistrationTraining.Models
@model RegViewAndRoleViewModel
@{
    ViewBag.Title = "SubmitRegDetails";
}
<div>

<div class="form-group">
                            <div class="col-md-offset-2 col-md-10">
                                <input type="submit" value="Submit" class="btn btn-primary" />

                            </div>
                        </div>
                @Html.ActionLink("Back to list", "SaveRegForm")
            </div>

Since you've mentioned that you are getting:

"System.InvalidOperationException: 'The model backing the 'eNtsaRegistration' context has changed since the database was created. Consider using Code First Migrations to update the database (go.microsoft.com/fwlink/?LinkId=238269).'"

You need to execute the following commands in the Package Manager Console in Visual Studio:

  1. Add-Migration {name}
  2. Update-Database

This happen when your entity models changed while the backing database hasn't been updated yet.

Alternatively, you can enable automatic migrations so that you won't have to do this for every model change during development. See the references below for more information.

enable-migrations –EnableAutomaticMigration:$true

Reference:

Code-Based Migration in Entity Framework 6 Automated Migration in Entity Framework 6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM