zzz projects Entity Framework 6 EF 6
Home EF Core 5 Articles Knowledge Base Online Examples
  • Home
  • EF Core 5
  • Articles
  • Knowledge Base
  • Online Examples

Articles

22 results for:

AsNoTracking

AsNoTracking...In the Entity Framework, the ...DbContext... keeps the track of all the changes done in the objects, so that the correct update is done to the database when the ...SaveChanges()... method of the context is called....When you execute the query, the Entity Framework puts these entities in a cache and tracks whatever changes are made on these entities until the ...SaveChanges()... method is called....But sometimes you do not need to track some entities, such as, retrieving the data f...
query as-no-tracking

Basic Query

Basic Query...test...In Entity Framework, querying data is executed against the ...DbSet... properties of the ...DbContext.......The ...DbSet... and ...IDbSet... implement IQueryable, so you can easily write a ...LINQ... query against the database....LINQ is a component in the .NET Framework that provides query capability against collections in C# or VB....LINQ queries can be written using query syntax or method syntax....The EF provider is responsible for translating the LINQ query into the act...
query

Check Object Existence

Check Object Existence...How to check if an object exists in the database?...What is the best way performance wise to check if the object exists in the database....StackOverflow Related Questions...Best way to check if object exists in Entity Framework?...Answer...For IEnumerable..., using ...Any()... method is the fastest way to check if the object exists in the database. It only has to look at one iteration and will return as soon as it finds a match....using (var context = new EntityContext()...
query

Eager Loading

Eager Loading...In Eager loading, a query for one type of entity also loads related entities as part of the query....Using eager loading, you don't need to execute a separate query for related entities....It means that requesting related data be returned along with query results from the database....It can be achieved by using the ...Include... method....In the following example, all the authors with their respective books are retrieved from the database by using eager loading....using (var cont...
query eager-loading

EF6 - Generic Query with LINQ

Generic queries with Linq...In the concept of object orientation, we always have in mind to create codes that can be reused within the application or even in different applications. In this context, I have been asked how it is possible to use ...LINQ..., which is totally object-oriented and strongly typed, and still create reusable codes....Taking advantage of this situation, I will show how it is possible to create a query method using ...LINQ..., which is used to query practically any entity (...
carloscds article linq query

Find

Find...Finds an entity with the given primary key values....If an entity with the given primary key values exists in the context, then it is returned immediately without requesting the store....Otherwise, a request is made to the store for an entity with the given primary key values and this entity, if found, is attached to the context and returned....If no entity is found in the context or the store, then null is returned....using (var context = new BookStore()) { var authors = context.Auth...
query find

Include

Include...In Entity Framework, the ...Include... method loads the related objects to include in the query results. It can be used to retrieve some information from the database and also want to include related entities....We have a simple model which contains two entities....public class Book { public int Id { get; set; } public string Title { get; set; } public int AuthorId { get; set; } [ForeignKey("AuthorId")] public Author Author { get; set; } } public class Author { ...
query include

Include Multiple Levels

Include Multiple Levels...How to Include multiple levels?...Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query. Eager loading is achieved by use of the Include method....The Include() method works quite well for Lists on objects, but what if there is a need for multiple levels of depth. For example, Customer contains a list of invoices and each invoice then contains a list of items....StackOverflow Related Questions...Entity Frame...
query include

Include Performance

Improve EF Include Performance...How to Improve Entity Framework Include Performance?...Entity Framework does a great job to generate SQL. However, they are often very far to be optimized....using (var ctx = new CustomerContext()) { var customers = ctx.Customers .Include(x => x.Orders.Select(y => y.Items.Select(z => z.Product)) .Include(x => x.Payments.Select(y => y.Items) .Include(x => x.Shippings.Select(y => y.Items) .ToList(); } ...StackOverflow Related Que...
query performance

Include with Where Clause

Include With Where Clause...How to Include with Where clause?...To retrieve some information from the database and also want to include related entities conditionally. For example, if we have a simple model containing two entities, Customers, and Invoices. Now to retrieve any customer information and also include all the invoices of that customer generated in last seven days....using (var context = new EntityContext()) { var fromDate = DateTime.Now.AddDays(-7); var customer = context.Cu...
query include

Joining

Joining...In SQL, a ...JOIN... clause is used to combine data from two or more tables, based on a related column between them. Similarly, in Entity Framework, the LINQ ...Join... is used to load data from two or more tables....It is always advisable to use navigational properties instead of LINQ ...Join... to query the target data....But if the entities do not have any navigational properties defined on them, then you will need the ...Join... operators...It can also be used to fine-tune the gene...
query

Lazy Loading

Lazy Loading...In Lazy loading, an entity or collection of entities is automatically loaded from the database when a property referring to that entity/entities is accessed for the first time....Lazy loading means delaying the loading of related data until you specifically request for it....The related data is transparently loaded from the database when the navigation property is accessed....It is enabled by default in Entity Framework, and you don't need to do anything....In the following exampl...
query lazy-loading

LINQ & SQL Injection

LINQ Prevent SQL Injection...Do LINQ is SQL Injection safe?...SQL injection is a code injection technique used to attack data-driven applications, and that might destroy your database. It is one of the most common web hacking techniques placing malicious code in SQL statements, via web page input....Now the question is will using LINQ to SQL help prevent SQL injection?...StackOverflow Related Questions...Will using LINQ to SQL help prevent SQL injection...Does LINQ To SQL C# eliminate any possib...
query

LINQ Dynamic

Use LINQ Dynamically...How to use LINQ dynamically?...LINQ enables you to write type-safe queries, and you will get compile-time checking of your LINQ queries. The type-safe queries are excellent for most scenarios, but there are some cases where you will need the flexibility to construct queries on the fly dynamically....For example, we have a very simple case where we want to sort customers, but we don't know how we want to order it until we actually get to runtime. So it's something the user ...
query linq linq-dynamic

LINQ Queries

LINQ Queries...A query is an expression that retrieves data from a data source. Queries are usually expressed in a specialized query language, such as SQL for relational databases and XQuery for XML....Language-Integrated Query (LINQ) offers a simpler, consistent model for working with data across various kinds of data sources and formats....In a LINQ query, you always work with programming objects....LINQ to Entities queries can be composed in two different syntaxes:...Query Expression Syntax:....
query linq

Projection

Projection...A projection is just a way of mapping one set of properties to another. In Entity Framework, it's a way of translating a full entity (database table) into a C# class with a subset of those properties....Projection queries improve the efficiency of your application because only specific fields are retrieved from your database....It is used to create a query that selects from a set of entities in your model but returns results that are of a different type....When you have the data, yo...
query

Raw SQL Queries

Raw SQL Queries...Why Raw SQL Queries?...Entity Framework allows you to query using LINQ with your entity classes, but sometimes you want to run queries using raw SQL directly against the database. You will need raw SQL queries where you cannot use LINQ to represent the query, for example;...If the generated SQL is not efficient enough....If you want to make use of existing stored procedures...If you just prefer to write your queries in SQL....DbSet.SqlQuery...The ...DbSet.SqlQuery... method ena...
query raw-sql

Stored Procedure

Stored Procedure...What is Stored Procedure?...A stored procedure a SQL code that you can save so that the code can be reused over and over again....If you have an SQL query that you write over and over again, save it as a stored procedure, and then call it to execute it....You can also pass parameters to a stored procedure so that the stored procedure can act based on the parameter value(s) that is passed....Stored Procedure in Entity Framework...The Entity Framework allows you to use stored pr...
query stored-procedure raw-sql

Substract Two Dates

Subtract Two Dates...How to subtract two dates?...How to determine the number of days between 2 dates using LINQ with Entity Framework? Let's take a look at a very simple example where we want to retrieve all those orders which are placed in the last 100 days....using (var ctx = new MyContext()) { int noOfDays = 30; var orders = context.Invoices .Where(i => DateTime.Now.Subtract(s.Date).TotalDays > noOfDays) .ToList(); } ...when you execute the above code, then it will th...
query

View Generated SQL

View Generated SQL...How to view the SQL Generated by Entity Framework...Entity Framework follows the following three steps....Translate C# code into SQL statements...Execute SQL on a target database...Return values back to C# objects....Now the question is how the generated SQL statements look like and how we can view the SQL executed on the database....StackOverflow Related Questions...How do I view the SQL generated by the Entity Framework?...Answer...The simple way of viewing the SQL generat...
query

When to use Include

When to Use Include...When to use Include() with Entity Framework?...When to use Include with Entity Framework and is it related to lazy loading?...Answer...Before jumping into the answer of when to use Include, let's have a look at the following simple example which contains three entities....public class Customer { public int CustomerID { get; set; } public string Name { get; set; } public virtual List<Invoice> Invoices { get; set; } } public class Invoice { public int InvoiceID { get; se...
query include

Why Async method are slow

Why EF Async Methods are Slow...Why EF async methods are slower than non-async?...Why Entity Framework async operation takes too much time as compared to a non-async operation?...StackOverflow Related Questions...Entity Framework async operation takes ten times as long to complete...Answer...The asynchronous version will always be slower than the synchronous version when there is no concurrency....It's doing all of the same work as the non-async version, but with a small amount of overhead added...
query performance

Page 1 of 2
  • 1
  • 2
  • »

Prime Library

Performance

  • Entity Framework Extensions
  • Entity Framework Classic
  • Bulk Operations
  • Dapper Plus

Expression Evaluator

  • C# Eval Expression
  • SQL Eval Function
More Projects...

Related

  • EF Extensions Online Benchmark
  • WIN an EF Extensions license
  • EF6 BatchSaveChanges for only $79
Save your entities 20× faster with EF Extensions
SaveChanges vs BulkSaveChanges

Try for free now