Results 1 to 6 of 6

Thread: [RESOLVED] Error on Class Entity Reference in LINQ

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    47

    Resolved [RESOLVED] Error on Class Entity Reference in LINQ

    Hi everyone,

    I'm getting the following error '*****' is a type, which in not valid in the given context
    for each of my table references except one.
    Here is the code for the query. I also attached two pictures, 1 showing the error and another showing the EF Model.Context.cs
    Code:
            private void ProjectsQry()
            {
                var context = new ProductivityEntities();
    
                var q = (from s in pt_Site
                         join t in pt_ProjectsSites on s.IDSite equals t.Site_id
                         join r in pt_Projects on t.Project_id equals r.IDProjects
                         join a in pt_ProjectSavings on r.IDProjects equals a.Projects_id
                         join l in pt_Personnel on a.Personnel_id equals l.IDPersonnel
                         where s.IDSite == 1
                         select new { l.FirstName, l.LastName, s.SiteName, r.ProjectName, r.StartName, r.EndDate, a.PlannedSavings, a.ActualSavings });
    Attached Images Attached Images   

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Error on Class Entity Reference in LINQ

    Shouldn't this:
    vb.net Code:
    1. from s in pt_Site
    be this:
    vb.net Code:
    1. from s in context.pt_Site
    The same goes for the other errors. You're accessing the entity collections on the context, not the entity type.

    By the way, it is convention to use singular names for entity types and plural names for entity collection classes, e.g. pt_Project and pt_Site for the types and pt_Projects and pt_Sites for the collection properties. Also, I doubt that there's justification for all those "pt_" prefixes.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    47

    Re: Error on Class Entity Reference in LINQ

    I'm actually using C# not VB but either way you were correct. That removed the errors. Thank you.
    By the way, it is convention to use singular names for entity types and plural names for entity collection classes, e.g. pt_Project and pt_Site for the types and pt_Projects and pt_Sites for the collection properties.
    I certainty want to follow proper convention. Thank you, when I move the db to production I will make the changes.
    Also, I doubt that there's justification for all those "pt_" prefixes.
    Sadly my justification is that our server admin appears to have a horrible habit of putting a lot of different projects on one SQL db. Then tells the developers to prefix there tables with their project name. I wasn't going to agrue with him since it took enough just to get access to the server.

    Anyways, I did some more research last night and was convinced that calling a stored procedure in SQL from the application has some advantages. I don't want to overstep and say it's better but I read a lot of good things, this read in-particular: https://stackoverflow.com/questions/...red-procedures

    Do you have an opinion on the matter?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Error on Class Entity Reference in LINQ

    Quote Originally Posted by justair07 View Post
    Sadly my justification is that our server admin appears to have a horrible habit of putting a lot of different projects on one SQL db. Then tells the developers to prefix there tables with their project name.
    That doesn't necessarily require you to use them in your C# code. If you use database-first then you can edit the type and member names in the model after you create the model and, if you use code-first, I believe that you can use attributes to specify the names to use in the database if they're different to those in the model.
    Quote Originally Posted by justair07 View Post
    Anyways, I did some more research last night and was convinced that calling a stored procedure in SQL from the application has some advantages. I don't want to overstep and say it's better but I read a lot of good things, this read in-particular: https://stackoverflow.com/questions/...red-procedures

    Do you have an opinion on the matter?
    As an application developer, I would generally recommend that stored procedures not be your first option. If the project is driven by a DBA or someone else database -focused then you might have to use them but, in general, I'd suggest that they only be used where doing so provides a tangible advantage, e.g. a SQL query performs significantly better than the equivalent LINQ to Entities query.

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    47

    Re: Error on Class Entity Reference in LINQ

    That doesn't necessarily require you to use them in your C# code. If you use database-first then you can edit the type and member names in the model after you create the model
    Did not know that, thank you!
    As an application developer, I would generally recommend that stored procedures not be your first option. If the project is driven by a DBA or someone else database -focused then you might have to use them but, in general, I'd suggest that they only be used where doing so provides a tangible advantage, e.g. a SQL query performs significantly better than the equivalent LINQ to Entities query.
    Thank you again, you're input is always appreciated!

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    47

    Re: [RESOLVED] Error on Class Entity Reference in LINQ

    hi jmc,

    So after MORE research I'm afraid that I will run into complications if I'm trying to return multiple resultsets. Looking at my query above, does that count as "returning multiple resultsets"? If so, I think I might want to stick to LINQ to Entities Query...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width