C#ASP.NET MVC:在此上下文中仅支持原始类型或枚举类型

时间:2018-03-20 19:43:33

标签: c# asp.net asp.net-mvc

我正在努力学习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."

2 个答案:

答案 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());

这里可以找到类似的问题(有多个复杂的类而不是一个):

`Only primitive types or enumeration are supported in this context` error whilst seeding ASP.NET MVC using AddOrUpdate match on multiple fields

答案 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中完成的,它很简单......