Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8725787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:06:50+00:00 2026-06-13T08:06:50+00:00

I am trying to initialize a database using Database.SetInitializer but I am getting a

  • 0

I am trying to initialize a database using Database.SetInitializer but I am getting a ‘Type argument does not inherit from or implement the constraint type ‘System.Data.Entity.DBContext’ Error when my initializer class DOES inherit from DBContext. Any ideas? I’m new to Code First EF by the way!

Context Class:

Imports System.Data.Entity
Imports System.Collections.Generic
Imports System.Data.Entity.Infrastructure
Imports System.Configuration

Public Class PropertyManagementContext
    Inherits DbContext

#Region "Constructor"
    Public Sub New()
        MyBase.New("Data Source=WSBS2K3SQL;Initial Catalog=AQUARIUS_TEST;Persist    Security Info=True;User ID=SomeUser;Password=SomePassword;")

    End Sub

#End Region

#Region "Tables"
    Public Complexes As DbSet(Of Complex)
    Public Buildings As DbSet(Of Building)
    Public Units As DbSet(Of Unit)
    Public Tenants As DbSet(Of Tenant)
#End Region

Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
    MyBase.OnModelCreating(modelBuilder)

    'Set the Primary Keys
    modelBuilder.Entity(Of Complex).HasKey(Function(c) c.ComplexId)
    modelBuilder.Entity(Of Building).HasKey(Function(b) b.BuildingId).HasKey(Function(b) b.ComplexId)
    modelBuilder.Entity(Of Unit).HasKey(Function(u) u.UnitId).HasKey(Function(u) u.BuildingId)
    modelBuilder.Entity(Of Tenant).HasKey(Function(t) t.TenantId)

    'Set Complex Primary Key to Identity(MS SQL Auto-Increment)
    modelBuilder.Entity(Of Complex).Property(Function(c) c.ComplexId) _
    .HasDatabaseGeneratedOption(ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)

End Sub

End Class

Initializer Class:

Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Data.Entity

Public Class PropertyManagementInitializer
    Inherits DropCreateDatabaseAlways(Of PropertyManagementContext)

Protected Overrides Sub Seed(context As PropertyManagementContext)
    MyBase.Seed(context)

    Dim complex As New Complex With {
        .Name = "SomeComplex",
        .City = "SomeCity",
        .Address = "SomeStreet",
        .PostalCode = "R2R2R2",
        .Type = "1"
    }
    context.Complexes.Add(complex)

    context.SaveChanges()

End Sub

End Class

Main Form where Database.SetInitializer() is called and giving the error:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try    
        Database.SetInitializer(Of PropertyManagementContext)(New PropertyManagementInitializer)

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:06:52+00:00Added an answer on June 13, 2026 at 8:06 am

    I have seen this before and for me it seemed that Visual Studio was in a funny way to fix I removed “Inherits DbContext” run a build and then re added “Inherits DbContext” and built solution again, Visual Studio no longer complained.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use Ant to initialize my Oracle database by using Ant.
I'm trying to access a database from within CLISP using clsql. Since I'm running
I am trying to delete a record from a database using the following code
I'm trying to initialize a RDF datastore using Jena and HSQLDB . From http://jena.sourceforge.net/DB/hsql-howto.html
I'm trying to setup a custom action to initialize my SQLite database using the
I am trying to seed some data using custom database initializers, but can't get
I am trying to initialize NSDictionary but it gives me following error: Program received
I am trying to initialize directX, but for some reason it can't create d3d
In my project, current approach is to create database if not already exists using
I'm trying to connect to a DB2 databse from a windows machine using XAMPP.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.