Setting schema name for DbContext

You can configure the default schema in OnModelCreating method of your custom inherited DbContext class like – public class MyContext: DbContext { public MyContext(): base(“MyContext”) { } public DbSet<Student> Students { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //Configure default schema modelBuilder.HasDefaultSchema(“Ordering”); } } Starting with EF6 you can use the HasDefaultSchema method … Read more

Invalid object name ‘dbo.__MigrationHistory’ using Database.Create; EF6.02 when connection string is passed in

This happens because EF does some probing for the __MigrationsHistory table. For instance you can use EF with an existing database that was not created using EF Migrations but EF has no way of knowing it so it tries to connect to the database and uses the table to check this. If the table does … 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

Entity Framework – Unable to load the specified metadata resource

I have had the same problem. After moving .edmx file with EF model to separate assembly mentioned error caused my headache: “Unable to load the specified metadata resource”. EF version 6.1 Solution: Old value: metadata=res://*/Data.DataModel.csdl New value: metadata=res://*/DataModel.csdl Initially .edmx was under project’s folder and I have moved it into project’s root.

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

Creating Unique Index with Entity Framework 6.1 fluent API

NOTE: Relevant to EF 6 You can use IndexAttribute as mentioned but with Fluent API instead of DataAnnotations which will do the trick: modelBuilder .Entity<Person>() .Property(t => t.Name) .HasColumnAnnotation( “Index”, new IndexAnnotation(new IndexAttribute(“IX_Name”) { IsUnique = true })); Unfortunately there is no other way to create unique indexes using Fluent API. There is an open … 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)