Sunday, June 26, 2016

fluentmigrator


Name space

using FluentMigrator;



ADD COLOM AS SQL STATEMENT 


Execute.Sql(@" IF NOT EXISTS(SELECT * FROM sys.columns  WHERE Name = 'CreditCode' 
AND OBJECT_ID = OBJECT_ID('RecFacility'))
                  BEGIN
                      ALTER TABLE RecFacility
                      ADD   CreditCode VArchar(10)  NULL ;
                      END;");



Sample class model


using System;

using System.Collections.Generic;
using System.Text;
using FluentMigrator;
using System.Security.Cryptography;
using System.Reflection;

namespace WebDbMigrations.Migrations
{
    [Migration(16180)]
    public class Step16180 : FluentMigrator.Migration
    {

        public override void Up()
        {
            Update.Table("DatabaseInfo").Set(new { VersionID = "1.618.0" }).AllRows();
            System.Console.WriteLine("Running migration 1.618.0");

           
        }

        public override void Down()
        {
            Update.Table("DatabaseInfo").Set(new { VersionID = "1.617.0" }).AllRows();
        }

    }
}


create new table


 var existsPlastiqHostedPayment = Schema.Table("PlastiqHostedPayment").Exists();
            if (!existsPlastiqHostedPayment)
            {
                Create.Table("PlastiqHostedPayment")
                    .WithColumn("Id").AsInt32().NotNullable().PrimaryKey().Identity()
                    .WithColumn("TimeCreated").AsDateTime().NotNullable()
                    .WithColumn("Merchant").AsString(50).NotNullable()
                    .WithColumn("Plastiq").AsString(50).NotNullable()
                    .WithColumn("UserPlastiqEmail").AsString(250).NotNullable()
                    .WithColumn("UsersPlastiqId").AsString(50).NotNullable()
                    .WithColumn("CardName").AsString(50).NotNullable()
                    .WithColumn("CardBrand").AsString(50).NotNullable()
                    .WithColumn("Zip").AsString(50).NotNullable()
                    .WithColumn("Amount").AsDecimal().NotNullable()
                    .WithColumn("MerchantAmount").AsDecimal().NotNullable()
                    .WithColumn("PlastiqAmount").AsDecimal().NotNullable()
                    .WithColumn("TotalAmount").AsDecimal().NotNullable()
                    .WithColumn("WebTransId").AsInt32().NotNullable()
                    .WithColumn("UserId").AsAnsiString(10).Nullable()
                    .WithColumn("CustCode").AsAnsiString(8).Nullable();

                Create.ForeignKey("FK_PlastiqHostedPayment_Users")
                    .FromTable("PlastiqHostedPayment").InSchema("dbo").ForeignColumns("UserId")
                    .ToTable("Users").InSchema("dbo").PrimaryColumns("UserId");
            }


------------------------------------------------------



Create table and insert values 

 var existsTownSiteInfoPaymentsPlastiq = Schema.Table("TownSiteInfoPaymentsPlastiq").Exists();
            if (!existsTownSiteInfoPaymentsPlastiq)
            {
                Create.Table("TownSiteInfoPaymentsPlastiq")
                    .WithColumn("Id").AsInt32().NotNullable().PrimaryKey().Identity()
                    .WithColumn("Code").AsString(10).NotNullable()
                    .WithColumn("MerchantId").AsString(50).NotNullable()
                    .WithColumn("IsEnabled").AsBoolean().NotNullable();

                Insert.IntoTable("TownSiteInfoPaymentsPlastiq").Row(new {
                    Code = "REC",
                    MerchantId=-1,
                    IsEnabled = false
                });

                Insert.IntoTable("TownSiteInfoPaymentsPlastiq").Row(new
                {
                    Code = "CP",
                    MerchantId = -1,
                    IsEnabled = false
                });

            }


Update table value


Update.Table("DatabaseInfo").Set(new { VersionID = "1.560.0" }).AllRows();



Execute sql query


   Execute.Sql("insert into RoleAccess(RoleId, Tag) select 'Staff', Tag from RoleAccess where RoleId = 'Employee' and tag not in (select tag from RoleAccess where RoleId = 'Staff');");










No comments:

Post a Comment

CS Events