Implementing one-to-zero-or-one relation in SQL Server

The 1-0..1 relation in your database is directly visible. It is built between Course and OnlineCourse tables where Course is principal in relation (1) and OnlineCourse is dependent with FK configured on CourseID. FK is also PK of the OnlineCourse = it must be unique and because of that it is 0..1. Database “always” uses … Read more

Getting exact error type in from DbValidationException

While you are in debug mode within the catch {…} block open up the “QuickWatch” window (ctrl+alt+q) and paste in there: ((System.Data.Entity.Validation.DbEntityValidationException)ex).EntityValidationErrors This will allow you to drill down into the ValidationErrors tree. It’s the easiest way I’ve found to get instant insight into these errors. For Visual 2012+ users who care only about the … Read more

Integration Testing Entity Framework code first with in-memory database

Use SQL Compact 4.0 (download both SqlCE and tools by web platform installer) – EF Code first has direct support for that. The only difference will be that your application will use connection string to big SQL Server: <add name=”MyDbContext” provider=”System.Data.SqlClient” connectionString= “Data Source=…;InitialCatalog=…;Integrated Security=SSPI” /> and your tests will use connection string to SQL … Read more

Entity Framework 4.1 DbSet Reload

This is not the way you are supposed to use DbContext, and because of that it is almost impossible to reload the data. Keeping a single context around for a long time is incorrect usage. The link will also answer why your tracked entities are not updated. You can selectively reload a single entity by … Read more

Entity Framework Code First Many to Many Setup For Existing Tables

Remove your Essence2EssenceSet model class. If junction table contains only keys of related entities participating in many-to-many relations it is not needed to map it as entity. Also make sure that your fluent mapping of many-to-many relations specifies schema for table: mb.Entity<Essence>() .HasMany(e => e.EssenceSets) .WithMany(set => set.Essences) .Map(mc => { mc.ToTable(“Essence2EssenceSet”, “Com”); mc.MapLeftKey(“EssenceID”); mc.MapRightKey(“EssenceSetID”); … Read more

Create association on non-primary key fields with Entity Framework 4.1 Fluent API

It is not possible. Relations in EF follows exactly same rules as in the database. It means that principal table must have unique identifier which is referenced by dependent table. In case of database the identifier can be either primary key or unique column(s) of principal table. Otherwise it is not valid relation. Entity framework … Read more

Model First with DbContext, Fails to initialize new DataBase

This is wrong connection string. Once you are using model-first / database-first (EDMX) you must use Entity connection string with referencing .ssdl, .msl and .csdl metadata files. Also be aware that you must create your database in design time when creating model from EDMX = you must generate SQL script and execute it to create … Read more

Using the Entry().CurrentValues.SetValues() is not updating collections

SetValues never updates navigation properties. When you execute your code it only knows about changes in simple / complex properties of the entity passed to your Update method. EF even don’t know about related entities of the entity passed to your Update method. You must manually tell EF about each change in your object graph … Read more