简体   繁体   中英

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.

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

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. 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? Make sure that all your properties and datatypes in your Project class correctly coincide with the Project database table fields.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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