我无法在 mvc:Sequence 中添加控制器不包含匹配元素
I can not dd controller in mvc:Sequence contains no matching element
我是 MVC5 编程的初学者。我正在使用 MVC5,Entity framework 6.I 有一些 class related.i 使用第一种代码方法。
在澄清关系之前,我可以毫无问题地添加控制器,但在那之后我就有问题了!例如看这张图片
add controller
error
这是我的代码:
location.cs
namespace NezamBarghLorestan.Models
{
[Table("TblLocation")]
public class Location
{
[Key]
public int ID { get; set; }
[Required]
[DisplayName("کشور")]
public string Country { get; set; }
[Required]
[DisplayName("استان")]
public string State { get; set; }
[Required]
[DisplayName("شهر")]
public string City { get; set; }
[DisplayName("منطقه")]
public string Zone { get; set; }
public virtual ICollection<Expert> experts{ get; set; }
}
}
和expert.cs
[Table("TblExpert")]
public class Expert:Person
{
[Required]
[DisplayName("کد عضویت")]
public string EnCode { get; set; } //شماره عضویت
[Required]
[DisplayName("شماره پروانه")]
public string LicenseNo { get; set; }//شماره پروانه
[Required]
[DisplayName("تاریخ اولین صدور پروانه")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{yyyy/MM/dd}", ApplyFormatInEditMode = true)]
[Column(TypeName = "datetime2")]
public DateTime? LicenseNoStart { get; set; }
[Required]
[DisplayName("تاریخ انقضای آخرین پروانه")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{yyyy/MM/dd}", ApplyFormatInEditMode = true)]
[Column(TypeName = "datetime2")]
public DateTime? LicenseNoEnd { get; set; }
[DisplayName("پایه طراحی")]
public Grade DesGrade { get; set; }
[Required]
[DisplayName("پایه نظارت")]
public Grade SupervisGrade { get; set; }
[DisplayName("امتیاز مثبت سالانه")]
public long Score { get; set; } //امتیاز مثبت سالانه
[DisplayName("امتیاز منفی سالانه")]
public int? NegScore { get; set; }//امتیاز منفی
[DataType(DataType.Password)]
[DisplayName("کلمه عبور")]
public string password { get; set; }
[DisplayName("شهر حوزه فعالیت ")]
public LorestanCity CityWork { get; set; }
[DisplayName("شهر محل صدور")]
public int? CityId { get; set; }
public virtual ICollection<ExpertsHistory> expertshistory { get; set; }
public virtual ICollection<Building> buildings { get; set; }
[ForeignKey("CityId")]
public virtual Location location { get; set; }
}
和数据访问层
public class NezamBarghDAL:DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Expert>().HasOptional<Location>(s => s.location)
.WithMany(s => s.experts).HasForeignKey(s => s.CityId);
}
public DbSet<Person> people { get; set; }
public DbSet<Expert> Experts { get; set; }
public DbSet<Facility> Facilitys { get; set; }
public DbSet<Manager> Managers { get; set; }
public DbSet<Owner> Owners { get; set; }
public DbSet<Location> locations { get; set; }
public DbSet<BuildingGroup> buildingGroups { get; set; }
public DbSet<FieldOfStudy> fieldOfStudies { get; set; }
public DbSet<Building> buidings { get; set; }
public DbSet<ExpertsHistory> expertHistories { get; set; }
}
在此测试之后,我从 class 中删除了关系,并且毫无问题地添加了控制器,并且我 return 恢复了与 class 的关系,但是在 运行 项目之后和打开控制器的 link 我收到此错误:
序列不包含匹配元素
Line 19: public ActionResult Index()
Line 20: {
Line 21: return View(db.locations.ToList());
Line 22: }
Line 23:
Source File: E:\WebSite\NezamBarghLorestan\NezamBarghLorestan\Controllers\LocationsController.cs Line: 21
我发现我的 problem.i 在一个数据注释中有错误。
我是 MVC5 编程的初学者。我正在使用 MVC5,Entity framework 6.I 有一些 class related.i 使用第一种代码方法。 在澄清关系之前,我可以毫无问题地添加控制器,但在那之后我就有问题了!例如看这张图片
add controller
error
这是我的代码: location.cs
namespace NezamBarghLorestan.Models
{
[Table("TblLocation")]
public class Location
{
[Key]
public int ID { get; set; }
[Required]
[DisplayName("کشور")]
public string Country { get; set; }
[Required]
[DisplayName("استان")]
public string State { get; set; }
[Required]
[DisplayName("شهر")]
public string City { get; set; }
[DisplayName("منطقه")]
public string Zone { get; set; }
public virtual ICollection<Expert> experts{ get; set; }
}
}
和expert.cs
[Table("TblExpert")]
public class Expert:Person
{
[Required]
[DisplayName("کد عضویت")]
public string EnCode { get; set; } //شماره عضویت
[Required]
[DisplayName("شماره پروانه")]
public string LicenseNo { get; set; }//شماره پروانه
[Required]
[DisplayName("تاریخ اولین صدور پروانه")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{yyyy/MM/dd}", ApplyFormatInEditMode = true)]
[Column(TypeName = "datetime2")]
public DateTime? LicenseNoStart { get; set; }
[Required]
[DisplayName("تاریخ انقضای آخرین پروانه")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{yyyy/MM/dd}", ApplyFormatInEditMode = true)]
[Column(TypeName = "datetime2")]
public DateTime? LicenseNoEnd { get; set; }
[DisplayName("پایه طراحی")]
public Grade DesGrade { get; set; }
[Required]
[DisplayName("پایه نظارت")]
public Grade SupervisGrade { get; set; }
[DisplayName("امتیاز مثبت سالانه")]
public long Score { get; set; } //امتیاز مثبت سالانه
[DisplayName("امتیاز منفی سالانه")]
public int? NegScore { get; set; }//امتیاز منفی
[DataType(DataType.Password)]
[DisplayName("کلمه عبور")]
public string password { get; set; }
[DisplayName("شهر حوزه فعالیت ")]
public LorestanCity CityWork { get; set; }
[DisplayName("شهر محل صدور")]
public int? CityId { get; set; }
public virtual ICollection<ExpertsHistory> expertshistory { get; set; }
public virtual ICollection<Building> buildings { get; set; }
[ForeignKey("CityId")]
public virtual Location location { get; set; }
}
和数据访问层
public class NezamBarghDAL:DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Expert>().HasOptional<Location>(s => s.location)
.WithMany(s => s.experts).HasForeignKey(s => s.CityId);
}
public DbSet<Person> people { get; set; }
public DbSet<Expert> Experts { get; set; }
public DbSet<Facility> Facilitys { get; set; }
public DbSet<Manager> Managers { get; set; }
public DbSet<Owner> Owners { get; set; }
public DbSet<Location> locations { get; set; }
public DbSet<BuildingGroup> buildingGroups { get; set; }
public DbSet<FieldOfStudy> fieldOfStudies { get; set; }
public DbSet<Building> buidings { get; set; }
public DbSet<ExpertsHistory> expertHistories { get; set; }
}
在此测试之后,我从 class 中删除了关系,并且毫无问题地添加了控制器,并且我 return 恢复了与 class 的关系,但是在 运行 项目之后和打开控制器的 link 我收到此错误: 序列不包含匹配元素
Line 19: public ActionResult Index()
Line 20: {
Line 21: return View(db.locations.ToList());
Line 22: }
Line 23:
Source File: E:\WebSite\NezamBarghLorestan\NezamBarghLorestan\Controllers\LocationsController.cs Line: 21
我发现我的 problem.i 在一个数据注释中有错误。