我应该创建手术网站。我创建了模型:Appointment,Doctor等等,然后我添加了带有UserId的AccountConstructor中的Model UserProfile,我想在需要的时候到处使用它。 我还没有使用迁移,我想在创建表时使用多次我尝试并且失败并且有很多错误。 我仍然得到关于约束外键的错误。 公式“var model = db.Appointments.ToList();”总是失败从中获取数据时 数据库所以我希望然后尝试基于我的模型创建数据库Localdb,通过我的dataSurg类继承自DbContext。
我的一些模特: dataSurg:的DbContext:
namespace OnlineSurg.Models
{
public class dataSurg : DbContext
{
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<Appointment> Appointments { get; set; }
public DbSet<Availability> Availabilitys { get; set; }
public DbSet<Doctor> Doctors { get; set; }
public DbSet<Medication> Medications { get; set; }
public DbSet<MedicationList> MedicationLists { get; set; }
public DbSet<Prescription> Prescriptions { get; set; }
public DbSet<Test> Tests { get; set; }
}
}
用户配置:
namespace OnlineSurg.Models
{
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
[DataType(DataType.Text)]
public string UserName { get; set; }
}
}
处方:
namespace OnlineSurg.Models
{
public class Prescription
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int prescriptionId { get; set; }
[Display(Name = "Medication List")]
[ForeignKey("MedicationList")]
public int medicationListId { get; set; }
public virtual MedicationList MedicationList { get; set; }
[ForeignKey("UserProfile")]
[DisplayFormat(NullDisplayText = "anonymous")]
public int UserId { get; set; }
public virtual UserProfile UserProfile { get; set; }
[DataType(DataType.Text)]
public string drug { get; set; }
[DataType(DataType.Text)]
public string dosage { get; set; }
[DataType(DataType.Text)]
public string quantity { get; set; }
[Display(Name = "last issued")]
[DataType(DataType.Text)]
public DateTime lastIssued { get; set; }
[DataType(DataType.Text)]
public string status { get; set; }
}
}
测试:
namespace OnlineSurg.Models
{
public class Test
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int testID { get; set; }
[ForeignKey("UserProfile")]
[DisplayFormat(NullDisplayText="anonymous")]
public int UserId { get; set; }
public virtual UserProfile UserProfile { get; set; }
[Required]
[Display(Name = "Doctor")]
[ForeignKey("Doctors")]
public int doctorId { get; set; }
public virtual Doctor Doctors { get; set; }
[Display(Name = "Test Name")]
public string testName { get; set; }
[Display(Name = "Date")]
[DataType(DataType.Date)]
public DateTime dateTime { get; set; }
[DataType(DataType.Text)]
[StringLength(1024)]
public string description { get; set; }
[DataType(DataType.MultilineText)]
public string result { get; set; }
}
}
预约:
namespace OnlineSurg.Models
{
public class Appointment
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int appointmentId { get; set; }
[Display(Name = "Date")]
[DataType(DataType.Date)]
[FutureDate(ErrorMessage = "Start date should be a future date")]
//[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime appointmentDate { get; set; }
[Display(Name = "Time")]
[DataType(DataType.Time)]
//[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime appointmentTime { get; set; }
[Display(Name = "Surgery Location")]
public string location { get; set; }
//string date = DateTime.Now.ToShortDateString();
//string time = DateTime.Now.ToShortTimeString();
//[MaxLength(100,ErrorMessage = "Max 100 characters")]
[Required]
[StringLength(1024)]
[Display(Name = "Reason (1024 Max)")]
public string description { get; set; }
[Required]
[Display(Name = "Doctor")]
[ForeignKey("Doctors")]
public int doctorId { get; set; }
public virtual Doctor Doctors { get; set; }
[ForeignKey("UserProfiles")]
public int UserId { get; set; }
public virtual UserProfile UserProfiles { get; set; }
}
public class FutureDateAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
return value != null && (DateTime)value > DateTime.Now;
}
}
}
医生:
namespace OnlineSurg.Models
{
public class Doctor
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int doctorId { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "First Name")]
public string firstName { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Last Name")]
public string lastName { get; set; }
//[Display(Name = "Picture")]
//public Image picture { get; set; }
[Display(Name = "Room")]
public int room { get; set; }
}
}
这是我的控制器:
AppointmentController:
namespace OnlineSurg.Controllers
{
public class AppointmentController : Controller
{
dataSurg db = new dataSurg();
public ActionResult Index(string searchTerm = null)
{
var model =
db.Appointments
.OrderBy(r => r.appointmentDate)
.Where(r => searchTerm == null || r.location.StartsWith(searchTerm))
.Select(r => new Appointment
{
appointmentId = r.appointmentId,
appointmentDate = r.appointmentDate,
appointmentTime = r.appointmentTime,
location = r.location,
description = r.description,
doctorId = r.doctorId,
UserId = r.UserId
});
return View();
}
//
// GET: /Appointment/Details/5
public ActionResult Details(int id)
{
return View();
}
//
// GET: /Appointment/Create
public ActionResult Create()
{
var username = User.Identity.Name;
var userrole = User.Identity;
ViewBag.doctorId = new SelectList(db.Doctors, "doctorId", "lastName");
ViewBag.UserId = new SelectList(db.UserProfiles, "UserId", "UserName");
return View();
}
//
// POST: /Appointment/Create
[HttpPost]
public ActionResult Create(Appointment appointment)
{
if (ModelState.IsValid) {
db.Appointments.Add(appointment);
db.SaveChanges();
return RedirectToAction("Index", new { id = appointment.appointmentId });
}
return View(appointment);
}
和HomeController:
namespace OnlineSurg.Controllers
{
public class HomeController : Controller
{
dataSurg db = new dataSurg();
public ActionResult Index()
{
var model = db.Appointments.ToList();
return View(model);
}
public ActionResult CheckResults(AccountController user)
{
ViewBag.Message = "Check Results";
var model = new Test()
{
};
return View();
}
public ActionResult Chat()
{
ViewBag.Message = "Chat";
return View();
}