How do I define a database view using Entity Framework 4 Code-First?

That’s because you cannot define database view using code-first approach. Database view is database construct which uses SQL Query on top of existing tables / functions. You can’t define such constructs using code first. If you want view you must create it manually by executing CREATE VIEW SQL script for example in custom initializer – … Read more

Composite Key with EF 4.1 Code First

You can mark both ActivityID and ActivityName properties with Key annotation or you can use fluent API as described by @taylonr. Edit: This should work – composite key defined with annotations requires explicit column order: public class ActivityType { [Key, Column(Order = 0)] public int ActivityID { get; set; } [Key, Column(Order = 1)] [Required(ErrorMessage … Read more

Using mvc-mini-profiler database profiling with Entity Framework Code First

This is now fully supported, check out the latest source or grab the package from nuget. You will need the MiniProfiler.EF package if you are using nuget. (1.9.1 and up) Supporting this involved a large set of modifications to the underlying proxy object to support acting as EF code first proxies. To add this support: … Read more

EntityType ‘IdentityUserLogin’ has no key defined. Define the key for this EntityType

In my case I had inherited from the IdentityDbContext correctly (with my own custom types and key defined) but had inadvertantly removed the call to the base class’s OnModelCreating: protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); // I had removed this /// Rest of on model creating here. } Which then fixed up my missing … Read more