IdentityServer / IdentityServer3.EntityFramework

EntityFramework persistence layer for IdentityServer3
Apache License 2.0
68 stars 97 forks source link

There is already an object named '__MigrationHistory' in the database. #95

Closed phillipzada closed 8 years ago

phillipzada commented 8 years ago

Getting an error when executing the Update-Database script on a clean install once upgraded to the new verision

There is already an object named '__MigrationHistory' in the database.

packages.config

  <package id="AutoMapper" version="4.2.1" targetFramework="net46" />
  <package id="EntityFramework" version="6.1.3" targetFramework="net46" />
  <package id="IdentityServer3" version="2.4.0" targetFramework="net46" />
  <package id="IdentityServer3.EntityFramework" version="2.4.2" targetFramework="net46" />
  <package id="Newtonsoft.Json" version="8.0.3" targetFramework="net46" />
  <package id="Owin" version="1.0" targetFramework="net46" />
phillipzada commented 8 years ago

Workaround - For anyone else experiencing this issue

  AddColumn("identityserver.Clients", "AllowAccessTokensViaBrowser", c => c.Boolean(nullable: false));
  AddColumn("identityserver.Clients", "RequireSignOutPrompt", c => c.Boolean(nullable: false));
  AddColumn("identityserver.Scopes", "AllowUnrestrictedIntrospection", c => c.Boolean(nullable: false));
phillipzada commented 8 years ago

I think its a bug in EF framework where it doesn't check the schema when it does it checks to see if the __MigrationHistory table exists.

I was able to solve this problem by just creating a Dummy __MigrationHistory table with the dbo schema, this tricked the EF6 generator for "creating the table"

    CREATE TABLE [dbo].[__MigrationHistory] (
       [MigrationId] [nvarchar](150) NOT NULL,
       [ContextKey] [nvarchar](300) NOT NULL,
       [Model] [varbinary](max) NOT NULL,
       [ProductVersion] [nvarchar](32) NOT NULL,
       CONSTRAINT [PK_dbo.__MigrationHistory] PRIMARY KEY ([MigrationId], [ContextKey])
    )

Alternatively if you want to automate it, you'll have to create a "Empty Migration" with a dummy class that uses the dbo schema and run that migration first to generate the relevant tables. Then run the scripts for the migration with the different schema and it should work.