locked
How to generate tables in the test environment? RRS feed

  • Question

  • User-1104215994 posted

    Hello guys,

    I implemented an ASP.NET web <g class="gr_ gr_82 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="82" data-gr-id="82">api</g> in my development computer. I created entities with EF6. Now I would like to deploy my API into <g class="gr_ gr_202 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="202" data-gr-id="202">test</g> environment. I would like to know how to generate my tables in the test database? Should I copy all the create scripts from SQL server management studio and run in the test? Is there an easy way?

    Best Regards.

    Tuesday, May 14, 2019 6:08 PM

Answers

  • User-893317190 posted

    Hi cenk1536,

    After a test , I find your thought is right.

    If you  use a new database , just change you dbcontext's connection string to your new database.

    Then you could run update-database –TargetMigration latestmigration, ef will apply all the history migrations.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 16, 2019 2:05 AM

All replies

  • User475983607 posted

    I implemented an ASP.NET web api in my development computer. I created entities with EF6. Now I would like to deploy my API into test environment. I would like to know how to generate my tables in the test database? Should I copy all the create scripts from SQL server management studio and run in the test? Is there an easy way?

    If you have create scripts then simply run the create scripts using SSMS while connected to the test database.

    Tuesday, May 14, 2019 6:12 PM
  • User-1104215994 posted

    I don't <g class="gr_ gr_9 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="9" data-gr-id="9">have</g> create scripts but I will get scripts from dev environment via <g class="gr_ gr_84 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" id="84" data-gr-id="84">SSMS</g>.

    Tuesday, May 14, 2019 6:18 PM
  • User-1104215994 posted

    Should I also copy the migrations data?

    0}The model backing the 'EPINMiddleWareAPIContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

    Tuesday, May 14, 2019 6:43 PM
  • User475983607 posted

    Should I also copy the migrations data?

    0}The model backing the 'EPINMiddleWareAPIContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

    If you are using code first then invoking the migrations is an option.   The Code First docs cover this concept quite well.  The link is in the error message you posted.

    Tuesday, May 14, 2019 7:09 PM
  • User-1104215994 posted

    I think you are talking about this;

    • Run the Update-Database -Script -SourceMigration: $InitialDatabase -TargetMigration: AddPostAbstract command in Package Manager Console

    Code First Migrations will run the migration pipeline but instead of actually applying the changes it will write them out to a .sql file for you. Once the script is generated, it is opened for you in Visual Studio, ready for you to view or save.

    Wednesday, May 15, 2019 5:13 AM
  • User-893317190 posted

    Hi cenk1536,

    If you want to recreate all the table's in the new test database, you could  change your dbcontext's connectionstring in web.config and migrate.

    If you haven't migrated, you could input enable-migrations , add-migrate and update-database.

    If you have migrated before, you could specify the magration through  TargetMigration parameter.

    For example , my first migration to create all the table is as follows, then I could input update-database -TargetMigration first to recreate all the tables.

    public partial class first : DbMigration
        {
            public override void Up()
            {
                CreateTable(
                    "dbo.financial",
                    c => new
                        {
                            id = c.Int(nullable: false, identity: true),
                            Salary_code = c.String(maxLength: 50),
                            fn_year = c.String(maxLength: 50),
                        })
                    .PrimaryKey(t => t.id);
                
                CreateTable(
                    "dbo.First",
                    c => new
                        {
                            id = c.Int(nullable: false, identity: true),
                            Info = c.String(maxLength: 50),
                            amount = c.Decimal(precision: 10, scale: 2),
                            catname = c.String(maxLength: 100),
                            desc = c.String(maxLength: 100),
                            extraProp = c.String(),
                        })
                    .PrimaryKey(t => t.id);
                
            }
            
            public override void Down()
            {
                DropTable("dbo.First");
                DropTable("dbo.financial");
            }
        }

    When meeting The model backing the 'xxx' context has changed since the database was created.

    It means that you have changed your model after the dbcontext has been created.

    For example, My model is as follows when the dbcontext has changed.

    [Table("First")]
        public partial class First
        {
            public int id { get; set; }
    
            [StringLength(50)]
            public string Info { get; set; }
    
            public decimal? amount { get; set; }
    
            [StringLength(100)]
            public string catname { get; set; }
    
            [StringLength(100)]
            public string desc { get; set; }
    
        }

    For some reason ,  I need to add another property named extra for this model.

        [Table("First")]
        public partial class First
        {
            public int id { get; set; }
    
            [StringLength(50)]
            public string Info { get; set; }
    
            public decimal? amount { get; set; }
    
            [StringLength(100)]
            public string catname { get; set; }
    
            [StringLength(100)]
            public string desc { get; set; }
    
            public string extraProp { get; set; }
        }

    Then when you use dbcontext after modifying this model, it will show this error.

    The solution is that you should add migration(if you are using code first).

    Open the console, input add-migration,  after you have named this migration , a migration file should occur in your Migrations folder.

    public partial class secod : DbMigration
        {
            public override void Up()
            {
                AddColumn("dbo.First", "extraProp", c => c.String());
            }
            
            public override void Down()
            {
                DropColumn("dbo.First", "extraProp");
            }
        }

    If you don't have Migration folder , please input enable-migrations first.

    Migration could only create database and tables for you, but it could add initial data for your tables.

    If you already have data in your dev database and want to move these data to test database , you could use ssms to backup and restore to your test database.

    https://www.eukhost.com/kb/backup-and-restore-ms-sql-server-2008-database/

    Best regards,

    Ackerly Xu

    Wednesday, May 15, 2019 9:05 AM
  • User-1104215994 posted

    I have migrated before so is it enough to target the latest migration? Please don't tell me I have to update <g class="gr_ gr_116 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="116" data-gr-id="116">database</g> for all the migrations in the history :)

    update-database –TargetMigration latestmigration

    Wednesday, May 15, 2019 10:02 AM
  • User-893317190 posted

    Hi cenk1536,

    After a test , I find your thought is right.

    If you  use a new database , just change you dbcontext's connection string to your new database.

    Then you could run update-database –TargetMigration latestmigration, ef will apply all the history migrations.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 16, 2019 2:05 AM