locked
LINQ and Entity Framework errors RRS feed

  • Question

  • User-953209254 posted

    I have two tables in SQL database:

    Challenger (ChallengerID, FirstName, LastName, Email)
    Rank (RankID, ChallengerID, Best, DateAcheieved)

     and using entity framework to access data. When I am trying to select columns from both tables i always get an error. What am I doing wrong? Thank you

    Dim db As New MemoryGameEntities
    Dim query = From t1 In db.Challengers
                            From t2 In db.Ranks
                            Where t1.ChallengerID = t2.ChallengerID And t2.Best = 1
                            Select (t1.ChallengerID, t1.FirstName, t1.LastName, t1.Email, t2.DateAchieved)

    Thursday, May 27, 2021 6:34 PM

All replies

  • User475983607 posted

    What is the error?

    Thursday, May 27, 2021 6:47 PM
  • User-953209254 posted

    "Only parameterless constructors and initializers are supported in LINQ to Entities". I can post entire message if you want

    Thursday, May 27, 2021 6:58 PM
  • User1535942433 posted

    Hi faremax,

    There are two solutions of your problem.

    1.Set some properties.Don't forget to add an empty constructor for the class.

    Dim db As New MemoryGameEntities
    Dim query = From t1 In db.Challengers
                            From t2 In db.Ranks
                            Where t1.ChallengerID = t2.ChallengerID And t2.Best = 1
                            Select (
                                     ID=t1.ChallengerID, 
                                     FName=t1.FirstName, 
                                     LName=t1.LastName, 
                                     Email=t1.Email, 
                                     DateAchieved=t2.DateAchieved)

    2. Use your constructor for initialization and not properties.

    Dim db As New MemoryGameEntities
    Dim query = From t1 In db.Challengers
                            From t2 In db.Ranks
                            Where (t1.ChallengerID = t2.ChallengerID And t2.Best = 1).tolist()
                            Select (t1.ChallengerID, t1.FirstName, t1.LastName, t1.Email, t2.DateAchieved)

    Best regards,

    Yijing Sun

    Friday, May 28, 2021 5:12 AM
  • User-953209254 posted

    Thank you for suggestions but unfortunately neither worked. I just don't have any experience with EF and at this point I don't think I want to waste any more time on it. If there is a solution to my question I would love to see it still but I am switching back to ADO.NET where I can have greater control over the whole process. Maybe I am wrong but I don't see any benefits to EF at this point. Why involve another process between front and back ends and let that process dictate you what it likes and what it does not like when you can write any statement you want and process the data you receive?

    Friday, May 28, 2021 3:15 PM
  • User1535942433 posted

    Hi faremax,

    Entity Framework instantiates objects for you. To be able to do so, it expects you to have a constructor without parameters in your class. Check if your class has such a constructor.

    Entity Framework is the all-out, no-holds-barred way to do it. This means you will take more time up-front, develop slower, and have more flexibility if you are working on something larger.

    Best regards,

    Yijing Sun

    Tuesday, June 1, 2021 7:57 AM