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

C#ASP.NET MVC:在此上下文中仅支持基元类型或枚举类型,c#,asp.net,asp.net-mvc,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; nam

我正在努力学习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; }
    }
}
然后,当我尝试更新数据库时,会收到这样的消息:

"Unable to create a constant value of type 'Przychodnia.Models.Clinic.Specialization'. 
Only primitive types or enumeration types are supported in this context."

您不能在
AddOrUpdate
方法中直接赋值
d.Specialization
,因为
d.Specialization
本身声明为复杂类,该方法要求表达式树中的所有属性参数声明为基元/简单类型。相反,您可以在
Doctor
类中创建
SpecializationID
唯一键属性,并将该属性的正确关联添加到
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; }
}
然后,您需要通过包括
SpecializationID
属性来调整
AddOrUpdate
方法表达式,以替换
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());
这里可能会发现类似的问题(多个复杂类而不是一个):


好了,我现在明白了!我会把答案贴出来,这样也许会对别人有所帮助。 我所要做的就是像这样改变种子方法

 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中完成,就这么简单…

只是一个猜测,但您不能将d.Specialization更改为d.Specialization.Name吗?你正在传递一个复杂类型的对象,而名称是一个基本类型。我想用完整的对象填充我的数据库,不仅仅是名称,我遵循本教程,它在视频中运行得非常好,不同之处在于我有更复杂的数据库模型,我可能会搞砸一些事情,指定我目前在28:30。当我创建上一个项目时,我用了一种不同的方式完成了它,这太复杂了,但我记得我传递了整个对象,它们工作得很好。哪行代码引发了异常?没有必要像这样更改代码,由于迁移文件自动检测关系并以适当的方式插入forein和主键,我不明白为什么我的数据库没有通过专门化填充,还有其他填充方法吗?
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());
 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();