Syntax for Entity Framework query to SQL Server 2017 Graph database
Asked Answered
N

2

8

Assume I'm working with the graph database from this sample (SQL Server 2017):

https://learn.microsoft.com/en-us/sql/relational-databases/graphs/sql-graph-sample

I have the following SQL query:

-- Find Restaurants that John likes
SELECT Restaurant.name
FROM Person, likes, Restaurant
WHERE MATCH (Person-(likes)->Restaurant)
AND Person.name = 'John';

I created a Model in C# using EF 6.1.3 and it autogenerates all the classes and everything from the database (EF Designer from database). This all works fine. I can even query all the people by using a simple method like:

public ICollection<People> ListPeople() => Entities.Peoples.ToList();

Now, if we come back to the original query, where I would like to find restaurants that John likes... how will I do this in Entity Framework? do I need to use a LINQ query or can I just call the entities? (presumably I can't because there doesn't seem to be any physical relationship between the tables, only by finding them in the edges)

I was thinking of something like

 public ICollection<Restaurant> ListRestaurantsLikedByPerson(string personName)
        {
            var result = from restaurant in Entities.Restaurants, person in Entities.Peoples, likes in Entities.likess
                where match (person - likes -> restaurant)
                and person.name = personName;

            return result;
        }

But this syntax is incorrect... how can I do this query?

Nan answered 13/10, 2017 at 15:53 Comment(0)
F
6

Entity Framework doesn't support the SQL server specific graph extensions.

Fairway answered 13/10, 2017 at 17:56 Comment(0)
D
6

David Glass has put up a pull request for EntityFramework Core to be able to support SQL Server Graph capabilities, you could try using his modified version of EFCore and see how that works for you. I am busy trying to use it for working with SQL Graph tables as well.

https://github.com/aspnet/EntityFrameworkCore/issues/8527 https://github.com/aspnet/EntityFrameworkCore/pull/13804

EDIT: This PR has since been closed due to major structural changes to the internals of the latest release of EF core, and is inactive at the time.

Drinkable answered 7/6, 2019 at 6:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.