0
我在DbContext類中遇到了DbSet問題。當我建立我的解決方案。它使我有以下錯誤:DbSet無法識別其實體
類型或命名空間名稱「學生」找不到(是否缺少using指令或程序集引用?)
以下是我的DbContext類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
namespace CodeFirstMigration.Models
{
public class StudentDbContext : DbContext
{
public StudentDbContext()
: base("StudentDbContext")
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<StudentDbContext>());
}
public DbSet<Student> Students { get; set; }
public DbSet<Department> Departments { get; set; }
}
}
Student類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
namespace CodeFirst.Models
{
public class Student
{
public Student()
{
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
[DataType(DataType.Date),
DisplayFormat(DataFormatString = "{0:dd/MM/yy}", ApplyFormatInEditMode = true)]
public DateTime? DateOfBirth { get; set; }
public string EmailId { get; set; }
public string Address { get; set; }
public string City { get; set; }
public Department Department { get; set; } // Navigation Property
}
}
部門級別:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace CodeFirst.Models
{
public class Department
{
public Department()
{
}
[Key]
public int Id { get; set; }
public string DepartmentName { get; set; }
public List<Student> Students { get; set; } // Navigation property
}
}
Student和Department類以及StudentDbContext都在同一個文件夾中。我試圖包括使用ProjectName.Models.Student和ProjectName.Models.Department但徒勞無功。請幫助!