Entity Framework Code First Using Guid as Identity with another Identity Column

This ended up working for me, Entity Framework 5. Turn off automatic migrations Migrate to create the initial table, no frills Declare the ClusterId as Identity (annotation) [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public override int ClusterId { get; set; } Migrate Declare the pk property Id as Identity after the other one has been updated [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public override Guid … Read more

Best way to incrementally seed data in Entity Framework 4.3

If you want to use entities to seed data you should use Seed method in your migrations configuration. If you generate fresh project Enable-Migrations you will get this configuration class: internal sealed class Configuration : DbMigrationsConfiguration<YourContext> { public Configuration() { AutomaticMigrationsEnabled = false; } protected override void Seed(CFMigrationsWithNoMagic.BlogContext context) { // This method will be … 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

DbContext AutoDetectChangesEnabled set to false detecting changes

Setting AutoDetectChangesEnabled to false doesn’t disable change tracking. (That’s what the AsNoTracking() extension method would do.) It just disables the automatic call of DetectChanges that would otherwise occur in many DbContext API methods. But DetectChanges isn’t the only method that participates in change tracking. However, if you don’t call it manually at the right places … Read more

What goes into DbContextOptions when invoking a new DbContext?

If you really want to create the context manually, then you can configure it like this: var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>(); optionsBuilder.UseSqlServer(Configuration.GetConnectionStringSecureValue(“DefaultConnection”)); _context = new ApplicationDbContext(optionsBuilder.Options); (The DbContextOptionsBuilder<ApplicationDbContext> class is the type of options argument in services.AddDbContext<ApplicationDbContext>(options =>). But in the controller, you don’t have access to Configuration object, so you would have to expose … Read more