【问题标题】:I can not dd controller in mvc:Sequence contains no matching element我无法在 mvc 中添加控制器:序列不包含匹配元素
【发布时间】:2016-11-02 18:22:10
【问题描述】:

我是 MVC5 编程的初学者。我正在使用 MVC5 和实体框架 6。我有一些相关的类。我使用第一种代码方法。 在澄清关系之前,我可以毫无问题地添加控制器,但之后我有问题!例如看这张图片

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; }


}

在此进行测试之后,我从类中删除了关系,并毫无问题地添加了控制器,然后我返回了与类的关系,但是在运行项目并打开控制器的链接后,我收到此错误: 序列不包含匹配元素

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 

【问题讨论】:

    标签: asp.net-mvc entity-framework controller ef-code-first entity-relationship


    【解决方案1】:

    我发现了我的问题。我在其中一个数据注释中出错了。

    【讨论】:

    • 嗨,你能分享一下你是如何解决这个问题的吗?
    猜你喜欢
    • 2013-03-21
    • 1970-01-01
    • 2014-05-23
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    相关资源
    最近更新 更多