Using Transactions or SaveChanges(false) and AcceptAllChanges()?

With the Entity Framework most of the time SaveChanges() is sufficient. This creates a transaction, or enlists in any ambient transaction, and does all the necessary work in that transaction. Sometimes though the SaveChanges(false) + AcceptAllChanges() pairing is useful. The most useful place for this is in situations where you want to do a distributed … Read more

How do I delete multiple rows in Entity Framework (without foreach)

EntityFramework 6 has made this a bit easier with .RemoveRange(). Example: db.People.RemoveRange(db.People.Where(x => x.State == “CA”)); db.SaveChanges(); Warning! Do not use this on large datasets! EF pulls all the data into memory, THEN deletes it. For smaller data sets this might not be an issue but generally avoid this style of delete unless you can … Read more

Entity Framework Timeouts

There is a known bug with specifying default command timeout within the EF connection string. http://bugs.mysql.com/bug.php?id=56806 Remove the value from the connection string and set it on the data context object itself. This will work if you remove the conflicting value from the connection string. Entity Framework Core 1.0: this.context.Database.SetCommandTimeout(180); Entity Framework 6: this.context.Database.CommandTimeout = … Read more

Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths – why?

Because Stage is required, all one-to-many relationships where Stage is involved will have cascading delete enabled by default. It means, if you delete a Stage entity the delete will cascade directly to Side the delete will cascade directly to Card and because Card and Side have a required one-to-many relationship with cascading delete enabled by … Read more

Error message ‘Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.’

This error has no true magic bullet answer. The key is to have all the information to understand the problem. Most likely a dynamically loaded assembly is missing a referenced assembly. That assembly needs to be in the bin directory of your application. Use this code to determine what is missing. using System.IO; using System.Reflection; … Read more

What are the best practices for using a GUID as a primary key, specifically regarding performance? [closed]

GUIDs may seem to be a natural choice for your primary key – and if you really must, you could probably argue to use it for the PRIMARY KEY of the table. What I’d strongly recommend not to do is use the GUID column as the clustering key, which SQL Server does by default, unless … Read more

Why use ICollection and not IEnumerable or List on many-many/one-many relationships?

Usually what you choose will depend on which methods you need access to. In general – IEnumerable<> (MSDN: http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx) for a list of objects that only needs to be iterated through, ICollection<> (MSDN: http://msdn.microsoft.com/en-us/library/92t2ye13.aspx) for a list of objects that needs to be iterated through and modified, List<> for a list of objects that needs … Read more

Conversion of a datetime2 data type to a datetime data type results out-of-range value

Short Answer This can happen if you do not initialize a value to a DateTime field; the field does not accept NULL values, and it’s a value type, so the default value of the non-nullable DateTime type will be used. Setting the value fixed it for me! Long Answer The value of default(DateTime) is DateTime.MinValue … Read more

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