简体   繁体   English

查询给出错误:字符串未被识别为有效的布尔值

[英]Query gives error: String was not recognized as a valid Boolean

I'm trying to get an object from my database with a generic repository. 我正在尝试从具有通用存储库的数据库中获取对象。 When I execute the code i'm receiving the errormessage: 当我执行代码时,我收到错误消息:

   String was not recognized as a valid Boolean. 

I have use the repository before with the Single() function and this doesn't give any errors. 我之前使用过带有Single()函数的存储库,但这没有给出任何错误。

Example working query 工作查询示例

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

The failing query 查询失败

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

The repository 仓库

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

The error pops up on the line with: return query.Single(where); 错误在以下行中弹出:return query.Single(where);

The project class 项目类

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

My database has the id of 2, so the query should definitely return a project object. 我的数据库的ID为2,因此查询肯定会返回一个项目对象。 I hope u have enough information, I will update my code if necessary. 我希望您有足够的信息,如有必要,我将更新我的代码。

Thanks in advance 提前致谢

EDIT 编辑

I have no idea what is the reason of this error, that's why I add this code, maybe it has something to do with the mapping: 我不知道此错误的原因是什么,这就是为什么我添加以下代码,也许与映射有关:

mapping 映射

        /*************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 category 班级类别

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

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

The type mismatch might actually be between other fields (ie: other than the ProjectID) in the database and your Project class? 类型不匹配可能实际上是在数据库中的其他字段(即ProjectID以外的其他字段)与您的Project类之间? Make sure that all your properties and datatypes in your Project class correctly coincide with the Project database table fields. 确保Project类中的所有属性和数据类型与Project数据库表字段正确匹配。

There might be an accidental mismatch eg: a boolean field in your Project class may be marked as a varchar field in the database or vice versa. 可能存在意外的不匹配,例如:Project类中的布尔字段可能在数据库中标记为varchar字段,反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM