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

How to update record using Entity Framework 6?

You’re trying to update the record (which to me means “change a value on an existing record and save it back”). So you need to retrieve the object, make a change, and save it. using (var db = new MyContextDB()) { var result = db.Books.SingleOrDefault(b => b.BookNumber == bookNumber); if (result != null) { result.SomeValue … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)