Sql 从用户处获取一些输入并使用实体框架插入多个表
我想在我的数据库中添加一些信息。问题是如何同时发布3个表的输入?我的桌子在下面。让我澄清一下 我有老师、工作时间和工作日。我希望首先从dropdownlist中选择教师姓名,然后从dropdownlist中选择日期,并写下工作时间,例如“09:00-17:00”。 在我提交这些信息之后,我希望看到所有这些信息都可以分别和关联地添加到数据库中Sql 从用户处获取一些输入并使用实体框架插入多个表,sql,asp.net,asp.net-mvc,entity-framework,linq,Sql,Asp.net,Asp.net Mvc,Entity Framework,Linq,我想在我的数据库中添加一些信息。问题是如何同时发布3个表的输入?我的桌子在下面。让我澄清一下 我有老师、工作时间和工作日。我希望首先从dropdownlist中选择教师姓名,然后从dropdownlist中选择日期,并写下工作时间,例如“09:00-17:00”。 在我提交这些信息之后,我希望看到所有这些信息都可以分别和关联地添加到数据库中 Sample scenario: John Reese Friday 09:00-17:00 Harold
Sample scenario: John Reese Friday 09:00-17:00
Harold Finch Monday 11:00-15:00
我可以从数据库中提取老师的名字,但同时在同一页中我想看到那天的名字。在我上面提到的所有这些选择之后,我想添加所有这些信息
我的创建控制器
public ActionResult Create()
{
var myTeacherList = (from teacher in db.Teachers.ToList()
select new SelectListItem
{
Text = teacher.Firstname + teacher.Lastname,
Value = teacher.Id.ToString(),
}).ToList();
var myDayNameList = (from day in db.WeekDays.ToList()
select new SelectListItem
{
Text = day.Name,
Value = day.Id.ToString(),
}).ToList();
ViewBag.TeacherId = myTeacherList;
ViewBag.DayId = myDayNameList;
return View();
}
我的创建控制器
<div class="form-horizontal">
<h4>Appointment</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.TeacherId, "Teacher Name", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m=>m.Teacher.Id,(List<SelectListItem>)ViewBag.TeacherId, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.TeacherId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Hours,"Working Hour", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Hours, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Hours, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
约会
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.TeacherId,“教师姓名”,htmlAttributes:new{@class=“controllabel col-md-2”})
@DropDownListFor(m=>m.Teacher.Id,(List)ViewBag.TeacherId,新的{@class=“form control”})
@Html.ValidationMessageFor(model=>model.TeacherId,“,new{@class=“text danger”})
@LabelFor(model=>model.Hours,“工作时间”,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Hours,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Hours,“,new{@class=“text danger”})
}
教师
namespace LanguageSchool.Models
{
using System;
using System.Collections.Generic;
public partial class Teacher
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Teacher()
{
this.Appointments = new HashSet<Appointment>();
this.Classes = new HashSet<Class>();
this.Languages = new HashSet<Language>();
}
public int Id { get; set; }
public string Description { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public System.DateTime DateOfStart { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Appointment> Appointments { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Class> Classes { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Language> Languages { get; set; }
}
}
名称空间语言学校
{
使用制度;
使用System.Collections.Generic;
公立部分班教师
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公共教师()
{
this.appoints=newhashset();
this.Classes=new HashSet();
this.Languages=new HashSet();
}
公共int Id{get;set;}
公共字符串说明{get;set;}
公共字符串名{get;set;}
公共字符串Lastname{get;set;}
公共字符串电子邮件{get;set;}
公用字符串电话{get;set;}
public System.DateTime DateOfStart{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection约会{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection类{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection语言{get;set;}
}
}
预约
namespace LanguageSchool.Models
{
using System;
using System.Collections.Generic;
public partial class Appointment
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Appointment()
{
this.WeekDays = new HashSet<WeekDay>();
}
public int Id { get; set; }
public int TeacherId { get; set; }
public string Hours { get; set; }
public virtual Teacher Teacher { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<WeekDay> WeekDays { get; set; }
}
}
名称空间语言学校
{
使用制度;
使用System.Collections.Generic;
公共部分班级任命
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公开任命()
{
this.WeekDays=new HashSet();
}
公共int Id{get;set;}
public int TeacherId{get;set;}
公共字符串小时数{get;set;}
公共虚拟教师{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection工作日{get;set;}
}
}
WeekDay.cs
namespace LanguageSchool.Models
{
using System;
using System.Collections.Generic;
public partial class WeekDay
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public WeekDay()
{
this.Class_WeekDay = new HashSet<Class_WeekDay>();
this.Appointments = new HashSet<Appointment>();
}
public int Id { get; set; }
public string Name { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Class_WeekDay> Class_WeekDay { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Appointment> Appointments { get; set; }
}
}
名称空间语言学校
{
使用制度;
使用System.Collections.Generic;
公共部分课工作日
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公众工作日
{
this.Class_WeekDay=new HashSet();
this.appoints=newhashset();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection类_工作日{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection约会{get;set;}
}
}
我们需要创建一个包含所有这些属性的ViewModel,以便在我们发出POST请求时,它们都被绑定,我们可以访问它们进行保存
约会中删除工作日
,并且
约会
自工作日
然后将其替换为AppointmentWeekday
确保在第一步之后运行迁移/更新数据库。
public class Appointment{
...
// REMOVE public virtual ICollection<WeekDay> WeekDays { get; set; }
// Add this
public virtual ICollection<AppointmentWeekday> AppointmentWeekdays {get;set;}
}
public class Weekday{
...
// REMOVE public virtual ICollection<Appointment> Appointments { get; set; }
// Add this
public virtual List<AppointmentWeekday> AppointmentWeekdays {get;set;}
}
// Add this
public class AppointmentWeekday{
public int AppointmentId {get;set;}
[ForeignKey("AppointmentId")]
public virtual Appointment Appointment {get;set;}
public int WeekdayId {get;set;}
[ForeignKey("WeekdayId")]
public virtual Weekday Weekday {get;set;}
}
在控制器中实例化它并将其传递给视图
让您的视图使用TeacherAppointViewModel
编辑视图,使用下面的代码
为了澄清,您想同时保存到多个表中,同时保存多条记录吗?是的,我实际上想通过DropDownList for选择一名教师和一天,并输入工作时间,以便将所有这些信息添加到数据库中。例如:我选择John教师和星期三,并输入09:00-15:00。在这个过程之后,我想看看所有这些信息都可以被添加到数据库RelationalySure中,我编辑过@JerdineSabio@JerdineSabio我将努力为你的答案,我会回答。Th
public class TeacherAppointmentViewModel{
public int TeacherId {get;set;}
public int DayId {get;set;}
public string Hours {get;set;}
}
public ActionResult Create()
{
var myTeacherList = (from teacher in db.Teachers.ToList()
select new SelectListItem
{
Text = teacher.Firstname + teacher.Lastname,
Value = teacher.Id.ToString(),
}).ToList();
var myDayNameList = (from day in db.WeekDays.ToList()
select new SelectListItem
{
Text = day.Name,
Value = day.Id.ToString(),
}).ToList();
ViewBag.TeacherId = myTeacherList;
ViewBag.DayId = myDayNameList;
// instantiate
TeacherAppointmentViewModel tvm = new TeacherAppointmentViewModel();
// pass to the view
return View(tvm);
}
@model TeacherAppointmentViewModel
<div class="form-horizontal">
<h4>Appointment</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.TeacherId, "Teacher Name", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m=>m.TeacherId,(List<SelectListItem>)ViewBag.TeacherId, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.TeacherId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.WeekdayId, "Day", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m=>m.WeekdayId,(List<SelectListItem>)ViewBag.WeekdayId, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.WeekdayId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Hours,"Working Hour", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Hours, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Hours, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
[HttpPost]
public ActionResult Create(TeacherAppointmentViewModel tvm){
// create appointment
Appointment a = new Appointment();
// assign teacher id and hours from viewmodel
a.TeacherId = tvm.TeacherId;
a.Hours = tvm.Hours;
// save appointment
db.Appointments.Add(a);
db.SaveChanges();
// create appointmentweekday
AppointmentWeekday aw = new AppointmentWeekday();
// assign properties
// since we've saved the appointment, we could use a.AppointmentId
aw.WeekdayId = tvm.WeekdayId;
aw.AppointmentId = a.AppointmentId; // appointment from earlier
// save appointmentweekday
db.AppointmentWeekdays.Add(aw);
db.SaveChanges();
}