Can I upgrade to a database created without migration in the Entity infrastructure?

I created a desktop application that uses SQL CE 4.0 and the Entity framework with Code First mode . So, first I installed the application on the system, it worked fine.

Now I have added several properties to the model class, so for this purpose I have included migrations with Enable-Migrationsand added a new migration using Add-Migration "MyMigration". It was also successful, and I was able to install the new version and update the database without any problems.

But now, when I install this installation on a system without an existing database, it fails, so after studying, I realized that I need to add the Initial migration . So, I canceled the changes in the model, and also deleted my SQLCE database file, added the initial migration, then Update-Database. Therefore, if I try to update the database using an old database that was created without enabling migration, then it does not work, it tries to apply the initial migration to the database, which should not match me.

Now, if I delete the file of the old database, and then Update-Database, then a new db will be created, and then my changes will be added to the model again and the expected migration will be added.

So, after this, a turn, I can install my application on an empty system, but it does not work on the system with the old database, that is, it does not update the database.

He is also trying to apply Initial-Migration, i.e. create all the tables again and not say that this table already exists, which is not expected.

+4
source share
4 answers

The fact of the matter is that it checks a table called Migrations, where I think it gets information about current compressibility and changes. If you make a few changes to the database without using EF, you will somehow reach the point where there will be a lot of clone conflicts.

,

:

 [Migration(201610250659)]
public class _201610250659_AddedMinimumValue_Prices : Migration
{
    public override void Up()
    {
        Alter.Table("Prices")
            .AddColumn("MinimunValue").AsInt32().NotNullable().WithDefaultValue(1);
    }

    public override void Down()
    {
        Delete.Column("MinimunValue")
            .FromTable("Prices");
    }
}
+2

update-database 

. , , -, - , , , .

+1

, , . EF, , .

,

- , , script .

script

    USE [TestStudent]
GO
/****** Object:  Table [dbo].[EmployeeInformation]    Script Date: 18-03-2017 19:12:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[EmployeeInformation](
    [Id] [int] NOT NULL,
    [Name] [nvarchar](50) NULL,
    [LastName] [nvarchar](50) NULL,
    [Designation] [nvarchar](50) NULL,
 CONSTRAINT [PK_EmployeeInformation] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[sample]    Script Date: 18-03-2017 19:12:41 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[sample](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [TableId] [int] NULL,
 CONSTRAINT [PK_sample] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

, , , , , .

enter image description here

script , , , , , , script ... , , .

enter image description here

, script...

    USE [TestStudent]
GO
/****** Object:  Table [dbo].[EmployeeInformation]    Script Date: 18-03-2017 19:22:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[EmployeeInformation]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[EmployeeInformation](
    [Id] [int] NOT NULL,
    [Name] [nvarchar](50) NULL,
    [LastName] [nvarchar](50) NULL,
    [Designation] [nvarchar](50) NULL,
 CONSTRAINT [PK_EmployeeInformation] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[sample]    Script Date: 18-03-2017 19:22:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sample]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[sample](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [TableId] [int] NULL,
 CONSTRAINT [PK_sample] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO

, , , , ...

enter image description here

, ...

+1

, , , , : MyMigration, , , , - :

1) script (BTW, ):

Update-Database -Script -SourceMigration: $InitialDatabase

2) script __MigrationHistory , .

3A) , . , .

IF @CurrentMigration < '201702231958592_Initial'
BEGIN
     -- objects already exist, but EF doesn't know this
     -- CREATE TABLE, etc.
END

3B) Entity __MigrationHistory, , . ! ( ).

INSERT [dbo].[__MigrationHistory]([MigrationId], [ContextKey], [Model], [ProductVersion])
VALUES (N'201702231958592_Initial', N'MyApp.Data.Migrations.Configuration',  0x1F8B08000000

4) 3 3 , , . , .

, , EF , .

As for your installer problem, you can either call this script through ADO.NET or use DbMigrator to migrate to the code.

+1
source

Source: https://habr.com/ru/post/1672433/


All Articles