Self-referencing many-to-many recursive relationship code first Entity Framework

By convention, Code First will take uni-directional associations as one to many. Therefore you need to use fluent API to let Code First know that you want to have a many to many self referencing association: protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Member>().HasMany(m => m.Friends).WithMany().Map(m => { m.MapLeftKey(“MemberId”); m.MapRightKey(“FriendId”); m.ToTable(“MembersFriends”); } ); }

Can you create sql views / stored procedure using Entity Framework 4.1 Code first approach

We support stored procedures in our Entity Framework Code First Migrations. Our approach is to create some folder to hold the .sql files (~/Sql/ for example). Create .sql files in the folder for both creating and dropping the stored procedure. E.g. Create_sp_DoSomething.sql and Drop_sp_DoSomething. Because the SQL runs in a batch and CREATE PROCEDURE.. must … Read more

How do I remove underscore of foreign key fields in code first by convention

I finally found an answer for this, by writing a custom convention. This convention works in EF 6.0 RC1 (code from last week), so I think it’s likely to continue to work after EF 6.0 is released. With this approach, the standard EF conventions identify the independent associations (IAs), and then create the EdmProperty for … Read more

Database in use error with Entity Framework 4 Code First

Your current context must have an opened connection to be able to drop the database. The problem is that there can be other opened connections which will block your db initializer. One very nice example is having opened any table from your database in management studio. Another possible problem can be opened connections in the … Read more

ASP.NET MVC3 and Entity Framework Code first architecture

If you want to use TDD (or any other testing approach with high test coverage) and EF together you must write integration or end-to-end tests. The problem here is that any approach with mocking either context or repository just creates test which can test your upper layer logic (which uses those mocks) but not your … Read more

C# Code-First migration, up/down?

The Up method upgrades your database from its current state (represented by your previous migration) to the state expected by your current code migration. The Down method does the reverse operation – it removes all the changes from the current migration and reverts database to the state expected by the previous migration. It’s like installing … Read more

How can I get my database to seed using Entity Framework CodeFirst?

This is what my DbContext classes all look like and they seed just fine: public class MyDbContext : DbContext { public DbSet<MyClass> MyClasses { get; set; } protected override void OnModelCreating (DbModelBuilder modelBuilder) { base.OnModelCreating (modelBuilder); modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention> (); // Add any configuration or mapping stuff here } public void Seed (MyDbContext Context) { #if DEBUG … Read more

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