How to create index in Entity Framework 6.2 with fluent configuration

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 do I enable EF migrations for multiple contexts to separate databases?

The 2nd call to Enable-Migrations is failing because the Configuration.cs file already exists. If you rename that class and file, you should be able to run that 2nd Enable-Migrations, which will create another Configuration.cs. You will then need to specify which configuration you want to use when updating the databases. Update-Database -ConfigurationTypeName MyRenamedConfiguration

EF Code First: How do I see ‘EntityValidationErrors’ property from the nuget package console?

I got annoyed by this recently too. I fixed it by putting a wrapper function in the Configuration class in the Seed method, and replaced calls to SaveChanges with calls to my function instead. This function would simply enumerate the errors within the EntityValidationErrors collection, and rethrow an exception where the Exception message lists the … Read more

Update Multiple Rows in Entity Framework from a list of ids

something like below var idList=new int[]{1, 2, 3, 4}; using (var db=new SomeDatabaseContext()) { var friends= db.Friends.Where(f=>idList.Contains(f.ID)).ToList(); friends.ForEach(a=>a.msgSentBy=’1234′); db.SaveChanges(); } UPDATE: you can update multiple fields as below friends.ForEach(a => { a.property1 = value1; a.property2 = value2; });

Unique Constraint in Entity Framework Code First

As far as I can tell, there’s no way to do this with Entity Framework at the moment. However, this isn’t just a problem with unique constraints… you may want to create indexes, check constraints, and possibly triggers and other constructs too. Here’s a simple pattern you can use with your code-first setup, though admittedly … Read more

Setting the default value of a DateTime Property to DateTime.Now inside the System.ComponentModel Default Value Attrbute

You cannot do this with an attribute because they are just meta information generated at compile time. Just add code to the constructor to initialize the date if required, create a trigger and handle missing values in the database, or implement the getter in a way that it returns DateTime.Now if the backing field is … Read more

DbArithmeticExpression arguments must have a numeric common type

Arithmetic with DateTime is not supported in Entity Framework 6 and earlier. You have to use DbFunctions*. So, for the first part of your statement, something like: var sleeps = context.Sleeps(o => DbFunctions.DiffHours(o.ClientDateTimeStamp, clientDateTime) < 24); Note that the DiffHours method accepts Nullable<DateTime>. Entity Framwork core (when used with Sql Server, maybe other db providers) … Read more

LINQ to Entities does not recognize the method

As you’ve figured out, Entity Framework can’t actually run your C# code as part of its query. It has to be able to convert the query to an actual SQL statement. In order for that to work, you will have to restructure your query expression into an expression that Entity Framework can handle. public System.Linq.Expressions.Expression<Func<Charity, … Read more

How do I clear tracked entities in entity framework

You can add a method to your DbContext or an extension method that uses the ChangeTracker to detach all the Added, Modified, and Deleted entities: public void DetachAllEntities() { var changedEntriesCopy = this.ChangeTracker.Entries() .Where(e => e.State == EntityState.Added || e.State == EntityState.Modified || e.State == EntityState.Deleted) .ToList(); foreach (var entry in changedEntriesCopy) entry.State = EntityState.Detached; … Read more