开发者

Exception raised when using a Linq query with Entity Framework

I removed a substantial amount of text from this question because I feel it needed to be more succinct. - Serg

Here's what I'm trying to do. User selects a State, then a ComboBox loads with Cities in that State, then when user selects a Сity a ListBox loads all projects from that City.

When running the method that's supposed to load the projects to the ComboBox, I receive this error:

Exception has been thrown by the target of an invocation. Inner Exception is: "{"Specified cast is not valid."}"valid."}"

This is the method that causes the exception:

private void LoadProjectsToListBox(long idCity)
{
    ProjectRepository projectRepo = new ProjectRepository();    

    //This line causes the error.
    var projects = projectRepo.FindAllProjects().Where(c => c.IDCity == 1).ToList();
}

In the Linq query, you'll see that I'm comparing against a hard coded number. 1, is a valid ID of a City that has Projects so it should work. The exception is only raised when I give the Linq query a City ID that has Projects. So it seems the error is raised when the query returns results.

Here is the SQL statements I used to generate the Project and City tables:

create table City
(
    ID integer primary key autoincrement,
    Name string,
    IDState integer references State(ID)
);

create table Project
(
    ID integer primary key autoincrement,
    Name string,
    StartDate text,
    IDManager integer references Manager(ID),
    IDCity integer references City(ID),
    IDDepartment integer references Department(ID),
    ContactNumber string,
    Description string
);

What strikes me as odd is that using the exact same pattern and access code to show a list of Departments (just another table in my database) everything works as expected. For example here's how I load Departments right now just for testing:

private void LoadProjectsToListBox(long idCity)
{
    ProjectRepository projectRepo = new ProjectRepository();
    DepartmentRepository departmentRepo = new DepartmentRepository();

    //Doesn't 开发者_Go百科work - raises exception.
    var projects = projectRepo.FindAllProjects().Where(c => c.IDCity == 1).ToList();

    //Works exactly as expected.
    var deps = departmentRepo.FindAllDepartments().Where(c => c.IDParentDepartment == 7).ToList();

    lstProjects.Items.Clear();
    foreach (var item in deps)
    {
        lstProjects.Items.Add(item.Name);
    }
}

Edit:

The FindAllDepartments(), FindAllCities(), FindAllProjects() methods are essentially the same.

DocumentsDBEntities db = new DocumentsDBEntities();

public IQueryable<Project> FindAllProjects()
{
    return db.Projects;
}

public IQueryable<City> FindAllCities()
{
    return db.Cities;
}

public IQueryable<Department> FindAllDepartments()
{
    return db.Departments;
}

As mentioned before, Department fetching works; Project fetching causes the exception.

Edit 3:

I fixed it! The problem was with using the datatype of 'Text' in the Project table. If I changed the datatype from that column from 'Text' to 'String' it works flawlessly. Can anyone tell me why?


I had the exact same problem last week! In my case the problem was that some of the tables I had pulled over to the .dbml file had a different database connection (test vs production) and even though the tables were (conceptually) the same, it was impossible to cast a table of type 'db1.table1' to 'db2.table1'. I don't know if that is the root of your problem or not, but I hope this helps in some way. Making sure that all my tables were coming from the exact same database connection was what solved it for me.


According to what I see here, Text datatype doesn't allow comparison with == (or OrderBy, for that matter). Unless I misunderstood the docs.


You most start your query with city (up to down):

var projects = projectRepo.FindAllCities()
    .Where(c => c.ID == 1)
    .Select(p => p.Projects)
    .ToList();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜