none
Pridanie tabulky pomocou migracie

    Dotaz

  • Zdravim,

    snazim sa pridat tabulku do svojej db pomocou migracie. (Migracie, kde som len menila nejake columns, resp. ich pridavala, malaza, mi tymto sposobom fungovali).

    Do svojho modelu som pridala triedu Order a vsetky jej stlpce. V package-manager console som dala Add-Migration AddOrder, ale vygenerovane metody Up a Down su prazdne. Tak som ich skusila napisat podla seba:

    namespace MvcSlap.Migrations
    {
        using System;
        using System.Data.Entity.Migrations;
        
        public partial class AddOrderTable : DbMigration
        {
            public override void Up()
            {
                CreateTable(
                    "dbo.Order",
                    c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        UserId = c.Int(nullable: false),
                        REF = c.String(nullable: false, maxLength: 10),
                        AMT = c.String(nullable: false, maxLength: 16),
                        CUR = c.String(nullable: false, maxLength: 3),
                        SIG = c.String(nullable: false, maxLength: 64),
                        RES = c.String(nullable: true, maxLength: 4),
                        TYP = c.String(nullable: true, maxLength: 4),
                        RES_NOT = c.String(nullable: true, maxLength: 4),
                        TID = c.String(nullable: true, maxLength: 10),
                        OID = c.String(nullable: true, maxLength: 10),
                        TSS = c.String(nullable: true, maxLength: 1),
                        CardID = c.String(nullable: true, maxLength: 36),
                        CardMask = c.String(nullable: true, maxLength: 19),
                        CardExp = c.String(nullable: true, maxLength: 4),
                        AuthNumber = c.String(nullable: true, maxLength: 7),
                        CardRecTxSec = c.String(nullable: true, maxLength: 1),
                        AcqResId = c.String(nullable: true, maxLength: 128),
                        SIG2 = c.String(nullable: true, maxLength: 64)
                    })
                    .PrimaryKey(t => t.Id)
                    .ForeignKey("dbo.UserProfile", t => t.UserId);
            }
    
            public override void Down()
            {
                DropForeignKey("dbo.Order", "UserId", "dbo.UserProfile");
                DropTable("dbo.Order");
            }
        }
    }
    

    Ked som to spustila, tak sa mi to vykonalo, vytvorilo tabulku Order, ale vyhodilo chybovu hlasku: "Automatic migration was not applied because it would result in data loss". Potom som vratila databazu do povodneho stavu a pokusila som sa o to znova s tym, ze teraz som do Configuration.cs v Migraciach pridala riadok: AutomaticMigrationDataLossAllowed = true

    public Configuration()
            {
                AutomaticMigrationsEnabled = true;
                AutomaticMigrationDataLossAllowed = true;
            }

    Kde robim chybu? Ako pridat tabulku pomocou migracie? Vdaka.

    8. prosince 2013 12:19

Odpovědi