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?
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.
SQL Server offers graph database capabilities to model many-to-many relationships.
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.
Entity Framework doesn't support the SQL server specific graph extensions.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With