How can set a default value constraint with Entity Framework 6 Code First?

Unfortunately the answer right now is ‘No’. But you can vote for Better support for default values EDIT 30 Mar 2015: It’s coming in EF7… Support database default values in Code First EDIT 30 Jan 2017: General support for default database values is part of EF Core (the new name for EF7)… Default values EDIT … Read more

How can I configure Entity Framework to automatically trim values retrieved for specific columns mapped to char(N) fields?

Rowan Miller (program manager for Entity Framework at Microsoft) recently posted a good solution to this which uses Interceptors. Admittedly this is only valid in EF 6.1+. His post is about trailing strings in joins, but basically, the solution as applied neatly removes trailing strings from all of the string properties in your models, automatically, … Read more

How can I catch UniqueKey Violation exceptions with EF6 and SQL Server?

With EF6 and the DbContext API (for SQL Server), I’m currently using this piece of code: try { // Some DB access } catch (Exception ex) { HandleException(ex); } public virtual void HandleException(Exception exception) { if (exception is DbUpdateConcurrencyException concurrencyEx) { // A custom exception of yours for concurrency issues throw new ConcurrencyException(); } else … Read more

Async PartialView causes “HttpServerUtility.Execute blocked…” exception

Child actions must be invoked synchronously. Issue 601 I am also not aware of any recent updates to the current MVC libraries allowing this functionality. A comment on Issue 601, hints at this functionality being added in MVC vNext, aka. MVC6. Child actions look to be replaced with ViewComponent which can be invoked asynchronously from … Read more

Save detached entity in Entity Framework 6

Yes, this is correct. This article describes various ways of adding and attaching entities, and it provides this example: var existingBlog = new Blog { BlogId = 1, Name = “ADO.NET Blog” }; using (var context = new BloggingContext()) { // The next step implicitly attaches the entity context.Entry(existingBlog).State = EntityState.Modified; // Do some more … Read more

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 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

The provider for the source IQueryable doesn’t implement IAsyncQueryProvider

I get stuck on this issue today and this lib resolve it for me https://github.com/romantitov/MockQueryable completely, please refer: Mocking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc. //1 – create a List<T> with test items var users = new List<UserEntity>() { new UserEntity{LastName = “ExistLastName”, DateOfBirth = DateTime.Parse(“01/20/2012”)}, … }; //2 – build mock by … Read more

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