在ASP中通过继承类在sql表中插入记录.asp.net MVC 5

本文关键字:记录 asp MVC 插入 net sql ASP 继承 | 更新日期: 2023-09-27 18:03:54

系统生成的Guardian类,继承自person

public class Guardian : Person
{
    public int GuardianId { get; set; }
    public string RelationWithApplicant { get; set; }
    public virtual Application Application { get; set; }
}

系统生成的Person基类

public partial class Person
{
    public Person()
    {
        this.Coaches = new HashSet<Coach>();
        this.Applications = new HashSet<Application>();
    }
    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Gender { get; set; }
    public System.DateTimeOffset BirthDate { get; set; }
    public string EmailId { get; set; }
    public string Height { get; set; }
    public string Weight { get; set; }
    public int LandlineNumber { get; set; }
    public int MobileNumber { get; set; }
    public string BuildingDetail { get; set; }
    public string StreetDetail { get; set; }
    public string AreaDetail { get; set; }
    public int CityId { get; set; }
    public int StateId { get; set; }
    public short CountryId { get; set; }
    public int AcademyRoleId { get; set; }
    public virtual AcademyRole AcademyRole { get; set; }
    public virtual Country Country { get; set; }
    public virtual State State { get; set; }
    public virtual City City { get; set; }
    public virtual ICollection<Coach> Coaches { get; set; }
    public virtual ICollection<Application> Applications { get; set; }
}

系统生成的应用程序类

public partial class Application
{
    public int ApplicationId { get; set; }
    public int PersonId { get; set; }
    public int CourseId { get; set; }
    public int ApplicationStatus { get; set; }
    public System.DateTime SubmissionDate { get; set; }
    public virtual Course Course { get; set; }
    public virtual Person Person { get; set; }
    public virtual Guardian Guardian { get; set; }
    public virtual Player Player { get; set; }
}

自定义类,通过它我试图从视图到控制器交换数据,反之亦然

public partial class NKBAApplication
{
    public Course CourseInfo { get; set; }
    public Person PersonInfo { get; set; }
    public Guardian GuardianInfo { get; set; }
    public CourseType CourseTypeInfo { get; set; }
    public Application ApplicationInfo { get; set; }
    public IEnumerable<SelectListItem> Countries { get; set; }
    public int SelectedCountry { get; set; }
    public IEnumerable<SelectListItem> States { get; set; }
    public int SelectedState { get; set; }
    public IEnumerable<SelectListItem> Cities { get; set; }
    public int SelectedCity { get; set; }
    public IEnumerable<SelectListItem> AcademyRoles { get; set; }
    public int SelectedRole { get; set; }

}

我是c# MVC新手。在这里,我试图通过申请表在个人表中插入记录。实际上,没有在表单中集成监护人,我的记录被保存了,但是当我试图在表单中添加监护人时,我不知道如何将记录插入到people表中,因为person记录存储在people中,而Guardian也继承自people表

控制器

    // GET: /Application/Create
    public ActionResult CreateApplication(int? id)
    {
        //            return RedirectToAction("Create");
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Course course = db.Courses.Find(id);
        if (course == null)
        {
            return HttpNotFound();
        }
        NKBAApplication nkbaApplication = new NKBAApplication();
        nkbaApplication.ApplicationInfo = new Application(); 
        nkbaApplication.ApplicationInfo.CourseId = course.CourseId;
        nkbaApplication.PersonInfo = new Person();
        nkbaApplication.CourseInfo = new Course();
        ViewBag.courseName = course.CourseType.CourseTypeName;
        ViewBag.courseStartDate = course.CourseStartDate;
        ViewBag.courseEndDate = course.CourseEndDate;
        ViewBag.courseLocation = course.CourseType.CourseTypeLocation;
        nkbaApplication.Countries = new SelectList(db.Countries, "CountryId", "CountryName");
        nkbaApplication.States = new SelectList(db.States, "StateId", "StateName");
        nkbaApplication.Cities = new SelectList(db.Cities, "CityId", "CityName");
        nkbaApplication.AcademyRoles = new SelectList(db.AcademyRoles, "AcademyRoleId", "AcademyRoleName");
        return View(nkbaApplication);
    }
    // POST: /Application/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult CreateApplication(int? id, NKBAApplication nkbaApplication)
    {
        if (ModelState.IsValid)
        {
            if (nkbaApplication.SelectedCountry == 0 || nkbaApplication.SelectedCity == 0 || nkbaApplication.SelectedRole == 0)
            {
                return HttpNotFound();
            }
            Country country = db.Countries.Find(nkbaApplication.SelectedCountry);
            if (country == null)
            {
                return HttpNotFound();
            }
            nkbaApplication.PersonInfo.CountryId = country.CountryId;
            State state = db.States.Find(nkbaApplication.SelectedState);
            if (state == null)
            {
                return HttpNotFound();
            }
            nkbaApplication.PersonInfo.StateId = state.StateId;
            City city = db.Cities.Find(nkbaApplication.SelectedCity);
            if (city == null)
            {
                return HttpNotFound();
            }
            nkbaApplication.PersonInfo.CityId = city.CityId;
            AcademyRole academyrole = db.AcademyRoles.Find(nkbaApplication.SelectedRole);
            if (academyrole == null)
            {
                return HttpNotFound();
            }
            nkbaApplication.PersonInfo.AcademyRoleId = 2;
            db.People.Add((Person)nkbaApplication.PersonInfo);
            db.SaveChanges();
            db.Guardians.Add((Guardian)nkbaApplication.GuardianInfo);
            db.SaveChanges();
            nkbaApplication.ApplicationInfo.CourseId = id.Value;
            nkbaApplication.ApplicationInfo.ApplicationStatus = 0;      // 0 is Applied
            nkbaApplication.ApplicationInfo.PersonId = nkbaApplication.PersonInfo.PersonId;
            nkbaApplication.ApplicationInfo.SubmissionDate = Convert.ToDateTime(DateTime.Now.ToString());
            db.Applications.Add((Application)nkbaApplication.ApplicationInfo);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(nkbaApplication);
    }

在ASP中通过继承类在sql表中插入记录.asp.net MVC 5

这是一个实体框架问题,而不是SQL或MVC。默认情况下,EF通过STI(单表继承)处理继承,也称为TPH(每层次表)。因此,当您说GuardianPerson时,EF创建了一个dbo.Persons表,其中有一个名为Discriminator的额外列。如果您保存Person,该列为空白,但如果您保存Guardian,该列将用字符串值"Guardian"填充。这就通知EF,当它检索记录时,它应该为该行实例化Guardian而不是Person。您还可以使用OfType<T>只选择某些类型,例如,db.Persons.OfType<Guardian>将只返回Discriminator列的值为"Guardian"的行,这些列都将被实例化为Guardian

您可以选择不同的继承策略,例如TPT(每个类型表)或TPC(每个具体类表)。TPT将产生dbo.Guardiansdbo.Persons表,其中dbo.Guardians将具有dbo.Persons中的一行的外键。TPC完全抛弃了多态性,只有两个完全独立的表,彼此之间没有链接。

鉴于此,那么,除非您自定义内容,创建Guardian也会创建Person。你不需要做任何额外的事情,你不应该试图同时添加这两个。换句话说,如果您想要一个Guardian,不要为Person的实例添加字段,只需为实例Guardian添加所有字段。