我正在努力学习ASP.NET MVC编程,但我发现了一个无法解决的问题。我正在编写代码 - 第一个数据库,我想通过Seed方法填充我的3个表,那里有模型代码:
//Doctor Table
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Przychodnia.Models.Clinic
{
public class Doctor
{
public int DoctorID { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string City { get; set; }
public string PostCode { get; set; }
public string Adress { get; set; }
public string Salary { get; set; }
[StringLength(9, MinimumLength = 9, ErrorMessage = "Phone number must be 9 characters long.")]
public string PhoneNumber { get; set; }
public string RoomNumber { get; set; }
public Specialization Specialization { get; set; }
public List<Appointment> Appointments { get; set; }
public List<Vacation> Vacations { get; set; }
}
}
//Patient Table
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Przychodnia.Models.Clinic
{
public class Patient
{
public int PatientID { get; set; }
[StringLength(11, MinimumLength = 11, ErrorMessage = "PESEL number must be 11 characters long.")]
public string PESEL { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string City { get; set; }
public string PostCode { get; set; }
public string Adress { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime BirthDate { get; set; }
public string InsuranceNumber { get; set; }
[StringLength(9, MinimumLength = 9, ErrorMessage = "Phone number must be 9 characters long.")]
public string PhoneNumber { get; set; }
public List<Appointment> Appointments { get; set; }
}
}
//Specialization Table
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Przychodnia.Models.Clinic
{
public class Specialization
{
public int SpecializationID { get; set; }
public string Name { get; set; }
}
}
现在,我有DummyData类,看起来像这样:
using Przychodnia.Models.Clinic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Przychodnia.Data
{
public class DummyData
{
public static List<Specialization> getSpecializations()
{
List<Specialization> specializations = new List<Specialization>()
{
new Specialization()
{
Name = "Dermatolog"
},
new Specialization()
{
Name = "Chirurg"
},
new Specialization()
{
Name = "Laryngolog"
}
};
return specializations;
}
public static List<Doctor> getDoctors(ClinicContext context)
{
List<Doctor> doctors = new List<Doctor>()
{
new Doctor()
{
Name = "Jan",
Surname = "Kowalski",
City = "Olsztyn",
PostCode = "123-123",
Adress = "Boeinga 2/14",
Salary = "5500",
PhoneNumber = "111222333",
RoomNumber = "18",
Specialization = context.Specializations.FirstOrDefault(o => o.Name == "Chirurg")
},
new Doctor()
{
Name = "Aleksy",
Surname = "Dimitriv",
City = "Warszawa",
PostCode = "300-200",
Adress = "Dymowskiego 2/14",
Salary = "10000",
PhoneNumber = "000999888",
RoomNumber = "101",
Specialization = context.Specializations.FirstOrDefault(o => o.Name == "Dermatolog")
},
new Doctor()
{
Name = "Juliusz",
Surname = "Petrarka",
City = "Kraków",
PostCode = "000-123",
Adress = "Mickiewicza 2/14",
Salary = "8500",
PhoneNumber = "333444222",
RoomNumber = "01",
Specialization = context.Specializations.FirstOrDefault(o => o.Name == "Laryngolog")
}
};
return doctors;
}
public static List<Patient> getPatients()
{
List<Patient> patients = new List<Patient>()
{
new Patient()
{
Name = "Anna",
Surname = "Pszczoła",
PESEL = "01234567890",
City = "Olsztyn",
PostCode = "123-123",
Adress = "Heweliusza 22",
BirthDate = DateTime.Now,
InsuranceNumber = "123123123ZZ",
PhoneNumber = "123321123"
},
new Patient()
{
Name = "Juliusz",
Surname = "Słowacki",
PESEL = "02030405060",
City = "Elbląg",
PostCode = "2-123",
Adress = "Mariusza 50",
BirthDate = DateTime.Now,
InsuranceNumber = "00000000123Z",
PhoneNumber = "000221122"
},
new Patient()
{
Name = "Karolina",
Surname = "Ogórek",
PESEL = "11104592831",
City = "Lublin",
PostCode = "123-2",
Adress = "Batorego 2",
BirthDate = DateTime.Now,
InsuranceNumber = "zzxxcc0002333",
PhoneNumber = "989231453"
},
};
return patients;
}
}
}
我已经通过添加一些行编辑了Configuration.cs种子方法:
context.Specializations.AddOrUpdate(
s => s.Name, DummyData.getSpecializations().ToArray());
context.SaveChanges();
context.Patients.AddOrUpdate(
p => new { p.PESEL, p.Name, p.Surname, p.City, p.PostCode, p.Adress, p.BirthDate, p.InsuranceNumber, p.PhoneNumber }, DummyData.getPatients().ToArray());
context.SaveChanges();
context.Doctors.AddOrUpdate(
d => new { d.Name, d.Surname, d.City, d.PostCode, d.Adress, d.Salary, d.PhoneNumber, d.RoomNumber, d.Specialization }, DummyData.getDoctors(context).ToArray());
context.SaveChanges();
然后,当我尝试更新数据库时,我收到了这样的消息:
"Unable to create a constant value of type 'Przychodnia.Models.Clinic.Specialization'.
Only primitive types or enumeration types are supported in this context."
答案 0 :(得分:1)
您不能直接在d.Specialization
方法中分配AddOrUpdate
,因为d.Specialization
本身被声明为复杂类,该方法期望表达式树中的所有属性参数都声明为原始/简单类型。相反,您可以在SpecializationID
类中创建Doctor
唯一键属性,并将该属性的正确关联添加到Specialization
类,如下所示:
public class Doctor
{
// other primitive type properties
// since EF is used, ForeignKeyAttribute may be used here
[ForeignKey("Specialization")]
public int SpecializationID { get; set; }
public virtual Specialization Specialization { get; set; }
}
然后,您需要通过添加AddOrUpdate
属性来替换SpecializationID
类来调整Specialization
方法表达式:
context.Doctors.AddOrUpdate(
d => new { d.Name, d.Surname, d.City, d.PostCode, d.Adress, d.Salary, d.PhoneNumber, d.RoomNumber, d.SpecializationID }, DummyData.getDoctors(context).ToArray());
这里可以找到类似的问题(有多个复杂的类而不是一个):
答案 1 :(得分:0)
context.Specializations.AddOrUpdate(
s => s.SpecializationID , DummyData.getSpecializations().ToArray());
context.SaveChanges();
context.Patients.AddOrUpdate(
p => p.PatientID, DummyData.getPatients().ToArray());
context.SaveChanges();
context.Doctors.AddOrUpdate(
d => d.DoctorID, DummyData.getDoctors(context).ToArray());
context.SaveChanges();
所以它只传递了原始类型,其余的填充是在DummyData.cs中完成的,它很简单......