c# - 查询给出错误 : String was not recognized as a valid Boolean

标签 c# asp.net-mvc asp.net-mvc-4 repository

我正在尝试使用通用存储库从我的数据库中获取一个对象。当我执行代码时,我收到错误消息:

   String was not recognized as a valid Boolean. 

我之前通过 Single() 函数使用过存储库,这没有给出任何错误。

示例工作查询

User usr = Adapter.UserRepository.Single(u => u.userEmail.Equals("thomas.vanlauwe@gmail.com"));

失败的查询

public ActionResult Display()
        {
            Project project = new Project();
            int id = 2;
            project = Adapter.ProjectRepository.Single(p => p.ProjectID.Equals(id));
            return View();
        }

存储库

public T Single(Expression<Func<T, bool>> where)
{
        try
        {
            IQueryable<T> query = IDbSet;
            //query = PerformInclusions(includeProperties, query);
            return query.Single(where);
        }
        catch (InvalidOperationException ex)
        {
            Console.WriteLine(ex);
            return null;
        }
    }

错误在以下行弹出:return query.Single(where);

项目类

public class Project
{
    public Project() {
        Categories = new List<Category>();
    }
    public int ProjectID { get; set; }
    [Display(Name = "Titel")]
    public string ProjectTitle { get; set; }
    [Display(Name = "Beschrijving")]
    public string ProjectDescription { get; set; }
    [Display(Name = "Deadline")]
    public Nullable<DateTime> ProjectDeadlineDate { get; set; }
    [Display(Name = "End register")]
    public Nullable<DateTime> ProjectEndRegisterDate { get; set; }
    [Display(Name = "Budget")]
    public double ProjectBudget { get; set; }
    public Boolean ProjectIsFixedPrice { get; set; } //Fixed price or budget / hour
    public int ProjectUrgent { get; set; }
    public Int16 ProjectDifficulty { get; set; }
    public Nullable<DateTime> ProjectCreatedDate { get; set; }
    public Boolean ProjectFinished { get; set; }
    public Int16 ProjectRating { get; set; }
    public string ProjectComment { get; set; }
    public int CompanyID { get; set; }

    public virtual ICollection<Category> Categories { get; set; }
}

我的数据库的 ID 为 2,因此查询肯定会返回一个项目对象。 我希望你有足够的信息,如有必要我会更新我的代码。

提前致谢

编辑

我不知道这个错误的原因是什么,这就是我添加这段代码的原因,也许它与映射有关:

映射

        /*************ProjectS**************/
        modelBuilder.Entity<Project>().HasKey(t => t.ProjectID);
        modelBuilder.Entity<Project>().HasMany(p => p.Categories)
            .WithMany(cat => cat.Projects)
            .Map(pc =>
                {
                    pc.ToTable("category_has_project");
                    pc.MapLeftKey("project_id");
                    pc.MapRightKey("category_id");
                }
        );

类(class)类别

public class Category
{
    public int CategoryID { get; set; }
    public string CategoryName { get; set; }

    public virtual ICollection<Project> Projects { get; set; }
}

最佳答案

类型不匹配实际上可能存在于数据库中的其他字段(即:ProjectID 除外)和您的 Project 类之间?确保 Project 类中的所有属性和数据类型与 Project 数据库表字段正确一致。

可能会出现意外的不匹配,例如:您的 Project 类中的 bool 字段可能被标记为数据库中的 varchar 字段,反之亦然。

关于c# - 查询给出错误 : String was not recognized as a valid Boolean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16925699/

相关文章:

c# - 我可以在 Xaml 中使用 Device.OnPlatform 设置设备特定 View 吗?

c# - 使用 Angular 上传文件时 HttpPostedFileBase 为空

java - 在 android studio 和 SignalR 中接收 List<Object>

c# - 为什么我的网站上的重定向会将我带到 azure.websites.net 而不是我的域?

c# - 首先使用 EF 5 数据库进行延迟加载

asp.net-mvc - 验证 Mvc 中的日期格式

c# - 保存具有多个父项的 EF 实例

c# - 是否有用于 MS Excel 操作(读/写)的 Dot NET (C#) 库?

c# - 在 C# 中使用子命令解析命令行

Javascript: TypeError: document.getElementById(...) 为 null 动态创建 <div> MVC4