Nurul Nurul - 22 days ago 11
Vb.net Question

Error setting Database Initializer: Type argument 'Configuration' does not inherit from or implement the constraint type 'DbContext'

I'm applying Code-First to an existing database in MS SQL Server 2012. I've done: right-click project -> add item -> ADO.net Entity Data Model -> Code-First from database. By this, all tables from the database is imported into my web API project.

Then, following this blog post, i've skipped steps 1 & 2 and proceeded to step 3 successfully. Then at step 4, since i'm doing a new project, not migrating a project from Database-First; instead of creating a new class to Set Database Initializer, somebody suggested that i put this snippet inside the constructor of my DbContext class:

Database.SetInitializer(new MigrateDatabaseToLatestVersion<Migrations.Configuration, WorkflowDbContext>());


This is after i implemented given code into my DbContext class:

Partial Public Class DbContext1
Inherits DbContext

Public Sub New()
MyBase.New("name=DbContext1")
Database.SetInitializer(New MigrateDatabaseToLatestVersion(Of Migrations.Configuration, DbContext1))
End Sub


But when i do this, errors at the keywords in my code above, i summarised below:



  • SetInitializer: "Type argument 'Configuration' does not inherit from or implement the constraint type 'DbContext'. "

  • Migrations.Configuration: "Type argument 'Configuration' does not inherit from or implement the constraint type 'DbContext'. "

  • DbContext1: "Type argument 'DbContext1' does not inherit from or implement the constraint type 'DbMigrationsConfiguration(Of Configuration)'. "




Right now, i'm modifying(adding foreign keys and navigation properties) the model classes that were automatically imported from the database using EF-code-first. Is this the cause to the problem i'm facing now?

Could someone point out to me what i should do to mend the problems?

P/S:


  1. I have only one DbContext and one Configuration in my project.

  2. Since the code above didn't work yet, i purposely skipped steps 4 for the sake of trying. I also skipped step 5 because my ConnectionString is already of type DbContext. So i proceeded to step 6. Surprisingly, it worked! I did Add-Migration initial, and now an 'initial' class has been created. But i'm quite worried if skipping those steps will cause an effect later on. Does it?


Answer

Change MigrateDatabaseToLatestVersion(Of Migrations.Configuration, DbContext1)) to MigrateDatabaseToLatestVersion(Of DbContext1,Migrations.Configuration)) I think you reversed the order of type argumen