mapping multiple tables to a single entity class in entity framework

You can use Entity Splitting to achieve this if you have the same primary key in both tables. modelBuilder.Entity<TestResult>() .Map(m => { m.Properties(t => new { t.Name, t.Text, t.Units /*other props*/ }); m.ToTable(“Result”); }) .Map(m => { m.Properties(t => new { t.Status, t.Analysis /*other props*/}); m.ToTable(“Test”); }); Here’s a useful article

Entity Framework Validation confusion – maximum string length of ‘128’

Default length of string field in code first is 128. If you are using EF validation it will throw exception. You can extend the size by using: [StringLength(Int32.MaxValue)] public string Body { get; set; } This post became somehow popular so I’m adding second approach which also works: [MaxLength] public string Body { get; set; … Read more

“Cannot drop database because it is currently in use”. How to fix?

The problem is that your application probably still holds some connection to the database (or another application holds connection as well). Database cannot be deleted where there is any other opened connection. The first problem can be probably solved by turning connection pooling off (add Pooling=false to your connection string) or clear the pool before … Read more

EF 4.1 loading filtered child collections not working for many-to-many

I could reproduce exactly the behaviour you describe. What I got working is this: context.Entry(student) .Collection(s => s.Courses) .Query() .Include(c => c.Students) .Where(c => c.Id == 1) .Load(); I don’t know why we should be forced also to load the other side of the many-to-many relationship (Include(…)) when we only want to load one collection. … Read more

DbSet table name

Extension methods for DbContext and ObjectContext: public static class ContextExtensions { public static string GetTableName<T>(this DbContext context) where T : class { ObjectContext objectContext = ((IObjectContextAdapter) context).ObjectContext; return objectContext.GetTableName<T>(); } public static string GetTableName<T>(this ObjectContext context) where T : class { string sql = context.CreateObjectSet<T>().ToTraceString(); Regex regex = new Regex(“FROM (?<table>.*) AS”); Match match = … Read more

What is the difference between IDbSet.Add and DbEntityEntry.State = EntityState.Added?

When you use dbContext.SomeEntitySet.Add(entityInstance); the status for this and all its related entities/collections is set to added, while dbContext.Entry(entityInstance).State = EntityState.Added; adds also all the related entities/collections to the context but leaves them as unmodified. So if the entity that you are trying to create has a related entity (and it’s value its not null), … Read more

Set database collation in Entity Framework Code-First Initializer

Solution with a command interceptor It is definitely possible, though it’s a bit of a hack. You can alter the CREATE DATABASE command with a command interceptor. Il will intercept all the commands sent to the database, recognize the database creation command based on a regex expression, and alter the command text with your collation. … Read more