Entity Framework: "Faking" a queryable navigation property

LINQ to entities translates LINQ queries into SQL queries which are then sent and executed in the database (server). That's why you get the exception that your get_purchases method cannot be translated into store expression (SQL).

To get books that were paid more than 90 you can use LINQ Join operator:

var books = from b in dBContext.Books
            join p in dBContext.Purchases on b.isbn equals p.isbn
            where p.price > 90
            select b;

Here is a great LINQ resource: LINQ 101

If you want to have EF-like navigation properties like Book.Purchases where there is no actual relation in the database, you'd have to create your own data repository layer that would be initialized with the DbContext which would be used in all properties and methods. Then you would be using that for all data access - i.e. you'd never use DbContext directly in your code. This is a good practice on larger projects, especially if you need to implement some business rules and data manipulation before it gets stored in the database. Look up "repository pattern" and you're sure to find many resources.

Here's one: Using the Repository Pattern with ASP.NET MVC and Entity Framework


Comments

  1. Dane

    • 2015/3/28

    Entity Framework (EF) Core is a lightweight, extensible, open source and cross-platform version of the popular Entity Framework data access 

  2. Yosef

    • 2019/12/28

    Entity Framework. Entity Framework (EF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write. Get it: Add this to your project and start taking advantage of this powerful O/RM.

  3. Josiah

    • 2017/4/25

    Entity Framework is an open-source ORM framework for .NET applications supported by Microsoft. It enables developers to work with data using objects of 

  4. Aryan

    • 2018/8/9

    Entity Framework documentation. Entity Framework Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations. EF Core works with many databases, including SQL Database (on-premises and Azure), SQLite, MySQL, PostgreSQL, and Azure Cosmos DB. Get Started.

  5. Kylan

    • 2017/5/30

    Learn Microsoft ADO.Net Entity Framework step by step. Learn Entity Framework Core, Entity Framework 6.x Code-First and DB-First approach using simple 

  6. Andre

    • 2019/2/7

    Entity Framework (EF) Core is a lightweight, extensible, open source and cross-platform version of the popular Entity Framework data access technology. EF Core can serve as an object-relational mapper (O/RM), which: Enables .NET developers to work with a database using .NET objects. Eliminates the need for most of the data-access code that

  7. Ira

    • 2020/8/2

    Entity Framework (EF) is an open source object–relational mapping (ORM) framework for ADO.NET. It was originally shipped as an integral part of .

  8. Valente

    • 2020/10/20

    Entity Framework is an open-source ORM framework for .NET applications supported by Microsoft. It enables developers to work with data using objects of domain specific classes without focusing on the underlying database tables and columns where this data is stored.

  9. Quentin

    • 2019/8/22

    Entity Framework Core EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations. EF Core 

  10. Montanari

    • 2018/9/8

    In this article. Entity Framework is made up of the EF Tools for Visual Studio and the EF Runtime. EF Tools for Visual Studio. The Entity Framework Tools for Visual Studio include the EF Designer and the EF Model Wizard and are required for the database first and model first workflows.

  11. Myers

    • 2017/5/15

    Learn Entity Framework Entity Framework by example. Learn EF6 with interactive tutorial.

  12. Johnson

    • 2015/11/3

    Entity Framework was first released in 2008, Microsoft's primary means of interacting between .NET applications and relational databases. Entity Framework is an Object Relational Mapper (ORM) which is a type of tool that simplifies mapping between objects in your software to the tables and columns of a relational database.

  13. Mylo

    • 2016/1/28

    Entity framework (hereafter, EF) is the framework ORM (object-relational mapping) that Microsoft makes available as part of the .NET development 

  14. Amir

    • 2017/11/27

    Entity Framework Core in Action teaches you how to access and update relational data from .NET applications. Following the crystal-clear explanations, 

  15. Marchetti

    • 2019/8/12

    Entity Framework 6 (EF6) is a tried and tested object-relational mapper for .NET with many years of feature development and stabilization.

Comments are closed.

Recent Posts