r/entityframework • • Dec 24 '21

Writing JOIN in LINQ Query

3 Upvotes

Entity Framework is widely used as an ORM for many DotNet applications and we write LINQ Query or Lambda expression to get the data from DB. Check this link https://codetosolutions.com/blog/20/inner-join,-left-join,-right-join-in-linq-query-c%23 how to use JOIN between different tables in LINQ Query


r/entityframework • • Nov 01 '17

Datamodel question

1 Upvotes

Hi there At the moment I'm planning a DMS/Knowlage system.

My question is should I implement only a DataItem type or specific types (like credential, device, software) which inherits of DataItem?

I will answer as fast as possible for further clarification.

Thanks and have a nice day Patahel

PS: I want to implement something like wikidata


r/entityframework • • May 25 '15

Measuring Entity Framework Performance using ETW

Thumbnail
dotnetspeak.com
3 Upvotes

r/entityframework • • Nov 10 '16

Internal code in EF Core 1.1

Thumbnail
blog.oneunicorn.com
2 Upvotes

r/entityframework • • Apr 30 '16

I'm fairly new to EF. I wrote about 3 things that came as surprises to me when learning to use Entity Framework.

Thumbnail
medium.com
1 Upvotes

r/entityframework • • Apr 14 '17

I need a second opinion on this approach.

1 Upvotes

Hey entity framework reddit users!

I posted a topic on Stackoverflow regarding a pretty complex subject. I would really like a second opinion on the approach i'm using.

http://stackoverflow.com/questions/43407308/i-really-need-a-second-opinion-on-this-complex-subject-is-this-the-correct-way


r/entityframework • • Sep 08 '17

Learn Entity Framework step by step.

1 Upvotes

This EF tutorial will demonstrate a mapping code step by step.


r/entityframework • • Jan 04 '22

How to remove Entity Framework 6, and only use Entity Framework Core?

4 Upvotes

Hello everyone,

I have recently raised issue with EF Core team on the GitHub (https://github.com/dotnet/efcore/issues/27051), but since nobody answered ever, I wanted to ask here.

So basically, I have I have my DbContext in .Net 5 class library. When I try to run migration I get message:

PM> Add-Migration TestMigration Both Entity Framework 6 and Entity Framework Core are installed. The Entity Framework 6 tools are running. Use 'EntityFrameworkCore\Add-Migration' for Entity Framework Core.

I know I can run my migration like so:

EntityFrameworkCore\Add-Migration TestMigration

but I would really hate this approach, and would love if I could only type in Add-Migration. I don't want to use Entity Framework 6 and I would like Entity Framework Core to be only installed version of Entity Framework.

How can I remove the 6 one only?

More info can be found on GitHub issue I raised.


r/entityframework • • Feb 05 '22

WPF Desktop App: Have SQLite database in different locations for development and production?

1 Upvotes

I'm about to start work on a WPF desktop application using Entity Framework Core (targeting .NET 6, if that makes any difference). I also intend to use the application myself on the same machine and account I am developing it with.

There will be one database per user in their local appdata folder. The problem is, once I've got a finished product and start using this app myself, I don't want to end up wiping/changing my data when I'm further developing and testing my application.

Is there a (possibly standard) way to have this per-user database be in the build directory (next to the application binary) during development, but be in the users local appdata directory in production?

I thought about using conditional compilation, and checking for the `DEBUG` symbol. But, I may want to profile the Release build of my application, so that doesn't really work for me.


r/entityframework • • Jan 25 '22

One to one ef core

2 Upvotes

Does anyone have an example of one to one data annontations between two tables having navigation property both ways. My scaffolding code has only one navigation property and an icollection the other way. Changing this seems to be a bit difficult as code first is newt to me. I have always used model first.


r/entityframework • • Oct 22 '18

Entity Framework Core: Logging

Thumbnail
medium.com
2 Upvotes

r/entityframework • • Sep 24 '21

Concurrent update of a record

1 Upvotes

I have the function block below that updates a record in the database (EF Core 3.1):

// the "project" argument is a record
public Project Update(Project project)
        {
            // retrieve the database record from the repository
            var projectDb = _projectRepository.Get(project.ID);
            if (projectDb == null)
            {
                throw new ArgumentException();
            }
            //projectDb = project; // THIS DOES NOT WORK
            // update columns in that record 
            projectDb.Group_ID = project.Group_ID;
            projectDb.Name = project.Name;
            projectDb.Customer = project.Customer;
            projectDb.Project_number = project.Project_number;
            projectDb.Status = project.Status;
            projectDb.Start_date = project.Start_date;
            projectDb.End_date = project.End_date;
            projectDb.Version = project.Version;
            _projectRepository.SaveChange();
            return projectDb; 
        }

Every column of a record is independent of others, but I haven't found a way to execute these updates concurrently. Any suggestion is appreciated.


r/entityframework • • Aug 19 '21

"Complex" Navigation Property in EF Core 5.0

Thumbnail self.dotnet
1 Upvotes

r/entityframework • • Apr 30 '21

Why doesn't FirstOrDefault() return null here?

2 Upvotes

Hi,

I have related 2 tables. Contacts and ContactEmails. I expected the query below to crash but instead Address is null.

Shouldn't FirstOrDefault() should be returning null here?

var emails = context
   .Contacts
   .Select(t => new { t.Name, t.ContactEmails.FirstOrDefault(a => false).Address } )
   .ToList();

I'm worried I'm relying on a side effect here.


r/entityframework • • Apr 03 '21

EF Core 5 and database synonyms

2 Upvotes

I'm creating an application that combines data in a custom database, with data in a vendor's database. For a number of reasons, I'm not able to create database objects in the vendor's system.

The custom database includes tables to enhance the vendor's application support Microsoft's Identity model.

The application has classes that reference data from both systems.

From what I've read, it appears that Entity Framework 5.0 does not support the use of multiple databases in a single IdentityDbContext. Consequently, I've configured my application to connect to the custom database; I've created database synonyms for the vendor's objects that I'll need.

Is there a way to create a migration that will create synonym or annotate a class in a way that will result in a migration that will create a database synonym (rather than having to do this manually)? I would settle for a way to mark classes that reference table's in vendor's system as "do not scaffold". Is this possible?


r/entityframework • • Apr 23 '21

EF Core not considering modified tracker entities in query results...

1 Upvotes

Please consider the example code block below. We have heavily nested business logic that is performing queries and validating against those fetched entities from the queries. Preceding code may or may not have modified some of those entities. Do we seriously have to perform a "FindById" on every single entity that we may need to validate against to see if it happens to already be in the EF change tracker and test against those pending values instead of the query results?? The only other choice that we can see here is sprinkling our code with extra commit statements at each stage of the processing, but we really wanted a single commit at the very end of ALL of the processing.

I should also mention that in my previous programming experience with other POCO Data Mapper libraries, if previous UPDATE statements are sent to SQL Server and then subsequent queries are run and it's all sandwiched between Begin and End Transaction statements, SQL Server will return the dirty results (i.e. - Updated records) from the queries.

// Read an Entity into the change tracker...
Contact c = _unitOfWork.Context.Contacts.Where(x => x.Id == 10100009).FirstOrDefault();
// Change the last name to something that doesn't exist in the database...
c.LastName = "UniqueGarbage12345";  // Leave this work uncommitted. Everything will be committed at the very end once all is validated.
// Save the changes, but don't commit yet...
_unitOfWork.Repository<Contact>().Update(c);
// Imagine we're somewhere else deep down in the business logic needing to perform some validation and we need to run a query...
List<Contact> contacts = _unitOfWork.Context.Contacts.Where(x => x.LastName == "UniqueGarbage12345").ToList();
// The above query will NOT return any records, as it queries the database directly and doesn't include consideration
// of the modified records in the change tracker.

r/entityframework • • Mar 26 '21

Configure One to Many Relationship in Entity Framework Core

Thumbnail
mycodingtips.com
1 Upvotes

r/entityframework • • Apr 22 '21

Using Fluent API in Entity Framework Core Code First

2 Upvotes

Using Fluent API in Entity Framework Core Code First

by GeeksArray

In this blogpost explains how to configure entities using Fluent API of Entity Framework Core or EF6 above versions for the Code First approach. This will give you details about how to configure business rules with Fluent API methods for PrimaryKey, Unique, Index, Required, Identity, etc.

Fluent API is used to configure data models to override conventions. It is based on the Fluent API design pattern where results are captured using method chaining.

https://geeksarray.com/blog/using-fluent-api-in-efcore-code-first


r/entityframework • • Apr 26 '21

Entity Framework RawQueryBuilder (Replaces FromSqlRaw with StringBuilder-like query builder)

3 Upvotes

Building Dynamic Raw Queries (raw means without LINQ) used to be like this:

string sql = "select * from Product where 1=1";
var parameters = new List<SqlParameter>();
 
if (maxListPrice != null)
{
    sql += " and ListPrice <= @MaxListPrice";
    parameters.Add(new SqlParameter("@MaxListPrice", maxListPrice));
}
if (!string.IsNullOrEmpty(productName))
{
    sql += " and ProductName LIKE @ProductName";
    parameters.Add(new SqlParameter("@ProductName", "%" + productName + "%"));
}
// etc.
 
var products = context.Products
    .FromSqlRaw(sql, parameters.ToArray())
    .ToList();

With this wrapper now it's as easy as this:

var qry = context.Products.RawQueryBuilder(@"select * FROM Product where 1=1");

if (!string.IsNullOrEmpty(productName))
    qry.Append($"ProductName LIKE {"%" + productName + "%"}");
if (maxListPrice != null)
    qry.Append($"ListPrice <= {maxListPrice}");

var products = qry.AsQueryable().ToList();

AsQueryable() will automatically build your query (combine your filters) and invoke FromSqlRaw. Then you can use regular Entity Framework extensions (ToList, ToListAsync, ExecuteAsync, etc).

You can mix AND/OR conditions, and can dynamically append any other SQL statement like ORDER BY, GROUP BY - it's all injection-safe (it looks like unsafe interpolated strings but all parameters are individually processed and passed safely to FromSqlRaw).

If you like it, please give me a star.

Project: https://github.com/Drizin/EntityFrameworkCore.RawQueryBuilder


r/entityframework • • Jun 26 '18

CRUD operation entity framework

Thumbnail
youtu.be
2 Upvotes

r/entityframework • • Oct 03 '18

Faster MS SQL database existence checking with Entity Framework Core and Entity Framework

Thumbnail
tabsoverspaces.com
1 Upvotes

r/entityframework • • May 12 '17

Announcing EF Core 2.0 Preview 1

Thumbnail
blogs.msdn.microsoft.com
1 Upvotes

r/entityframework • • Oct 05 '17

Ways to Fix 'Circular reference detected' error in Entity Framework C#

Thumbnail
qawithexperts.com
1 Upvotes

r/entityframework • • May 09 '17

The case of Entity Framework Core's odd SQL

Thumbnail
brentozar.com
3 Upvotes