DbSet.Attach(entity) vs DbContext.Entry(entity).State = EntityState.Modified

When you do context.Entry(entity).State = EntityState.Modified;, you are not only attaching the entity to the DbContext, you are also marking the whole entity as dirty. This means that when you do context.SaveChanges(), EF will generate an update statement that will update all the fields of the entity. This is not always desired. On the other … Read more

How can I delete 1,000 rows with EF6?

EF 6 as far as I know introduced the .RemoveRange() option on your DbContext. So in short, you can do something like the following: var db = new MyDbContext(); var itemsToDelete = db.MyTable.Where(x=>!x.active); db.MyTable.RemoveRange(itemsToDelete); db.SaveChanges(); So instead of having to do any type of foreach, you can utilize this new extension method. With your Unit … Read more

Entity Framework 6 Code First Custom Functions

You should be able to use a scalar SQL function in your Where criterias with CodeFirstStoreFunctions Assuming you want to map SQL function [dbo].[LatLongDistanceCalc], and according to the test suite: public class MyDataContext: DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) { //… modelBuilder.Conventions.Add(new FunctionsConvention(“dbo”, this.GetType())); } // “CodeFirstDatabaseSchema” is a convention mandatory schema name // … Read more

Dynamic MySQL database connection for Entity Framework 6

Entity Framework 6 offers some handy subtle changes which aid in both getting MySQL working and also creating dynamic database connections. Getting MySQL working with Entity Framework 6 First, at the date of my answering this question, the only .Net connector drivers compatible with EF6 is the MySQL .Net Connectior 6.8.1 (Beta development version) which … Read more

How to update record using Entity Framework Core?

To update an entity with Entity Framework Core, this is the logical process: Create instance for DbContext class Retrieve entity by key Make changes on entity’s properties Save changes Update() method in DbContext: Begins tracking the given entity in the Modified state such that it will be updated in the database when SaveChanges() is called. … Read more

Does Entity Framework support parallel async queries? [duplicate]

This is not supported as per the specifications of version 6. This should throw a DbConcurrencyException exception saying A second operation started on this context before a previous asynchronous operation completed. Use ‘await’ to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to … Read more

Enable Entity Framework 6 for MySql (C#) in WinForms of Microsoft Visual Studio 2013

First of all, we don’t even need to install the mysql-installer-community-5.7.3.0-m13.msi. Install the latest mysql-visualstudio-plugin Install the latest mysql-connector-net New C# .Net 4.5 Framework WinForms (for 4.0 it should work based on Does Entity Framework 6 support .NET 4.0? ) Install 4 Nuget Packages (follow sequence, if you install Mysql.Data.Entities before EntityFramework, it will resolve … Read more

Entity Framework 6: audit/track changes

If using EF6’s DbContext you can use ChangeTracker in SaveChanges override to find added/modified entities of custom type, for example IAuditedEntity. public interface IAuditedEntity { string CreatedBy { get; set; } DateTime CreatedAt { get; set; } string LastModifiedBy { get; set; } DateTime LastModifiedAt { get; set; } } public override int SaveChanges() { … Read more

Setting unique Constraint with fluent API?

On EF6.2, you can use HasIndex() to add indexes for migration through fluent API. https://github.com/aspnet/EntityFramework6/issues/274 Example modelBuilder .Entity<User>() .HasIndex(u => u.Email) .IsUnique(); On EF6.1 onwards, you can use IndexAnnotation() to add indexes for migration in your fluent API. http://msdn.microsoft.com/en-us/data/jj591617.aspx#PropertyIndex You must add reference to: using System.Data.Entity.Infrastructure.Annotations; Basic Example Here is a simple usage, adding an … Read more