Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax for Entity Framework query to SQL Server 2017 Graph database

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

https://docs.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?

like image 910
gbdavid Avatar asked Oct 13 '17 15:10

gbdavid


People also ask

What is graph table SQL Server 2017?

SQL Graph Database A graph is a collection of node and edge tables. Node or edge tables can be created under any schema in the database, but they all belong to one logical graph. A node table is collection of similar type of nodes. For example, a Person node table holds all the Person nodes belonging to a graph.

Can SQL be used as a graph query language?

SQL Server offers graph database capabilities to model many-to-many relationships.

Can we plot graph in SQL?

Normally, the best way to provide a graph based on data in SQL Server is to create it in a BI tool like SSRS. SSRS is a highly versatile tool which allows for easy creation of a wide array of charts and graphs, including Trellis Charts, bullet graphs, statistical box plot charts, and dashboards to organize all of it.


2 Answers

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

like image 137
NetMage Avatar answered Oct 01 '22 20:10

NetMage


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.

like image 43
Bennie van der Walt Avatar answered Oct 01 '22 21:10

Bennie van der Walt