Database.BeginTransaction vs Transactions.TransactionScope [duplicate]

I found out the answer in Entity Framework 6’s documentation: With the introduction of EF6, Microsoft recommends to use new API methods: Database.BeginTransaction() and Database.UseTransaction(). Although System.Transactions.TransactionScope is still very well supported, it is no longer necessary for most users of EF6. While Database.BeginTransaction() is used only for database related operations transaction, System.Transactions.TransactionScope, in addition …

Read more

How to create index in Entity Framework 6.2 with code first

Well 26.10.2017 Entity Framework 6.2 was officially released. It includes a possibility to define indexes with ease via Fluent API. Ho it is to use was already announced in the beta of 6.2. Now you can use the HasIndex() method, followed by IsUnique() if it should be an unique index. Just a small comparison (before/after) …

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

There is already an object named in the database

it seems there is a problem in migration process, run add-migration command in “Package Manager Console”: Add-Migration Initial -IgnoreChanges do some changes, and then update database from “Initial” file: Update-Database -verbose Edit: -IgnoreChanges is in EF6 but not in EF Core, here’s a workaround: https://stackoverflow.com/a/43687656/495455

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 are people unit testing with Entity Framework 6, should you bother?

This is a topic I’m very interested in. There are many purists who say that you shouldn’t test technologies such as EF and NHibernate. They are right, they’re already very stringently tested and as a previous answer stated it’s often pointless to spend vast amounts of time testing what you don’t own. However, you do …

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

How to connect to LocalDB in Visual Studio Server Explorer?

In Visual Studio 2012 all I had to do was enter: (localdb)\v11.0 Visual Studio 2015 and Visual Studio 2017 changed to: (localdb)\MSSQLLocalDB as the server name when adding a Microsoft SQL Server Data source in: View/Server Explorer/(Right click) Data Connections/Add Connection and then the database names were populated. I didn’t need to do all the …

Read more