Created
September 27, 2018 03:36
-
-
Save danielplawgo/98d7e1b96586a573f0d964a3c3ca80e5 to your computer and use it in GitHub Desktop.
Migracja schematu bazy danych w Entity Framework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class AddCategory : DbMigration | |
{ | |
public override void Up() | |
{ | |
CreateTable( | |
"dbo.Categories", | |
c => new | |
{ | |
Id = c.Int(nullable: false, identity: true), | |
Name = c.String(), | |
}) | |
.PrimaryKey(t => t.Id); | |
AddColumn("dbo.Products", "CategoryId", c => c.Int(nullable: false)); | |
Sql("INSERT INTO dbo.Categories SELECT DISTINCT Category FROM dbo.Products;"); | |
Sql("UPDATE p SET p.CategoryId = (SELECT c.Id FROM dbo.Categories c WHERE c.Name = p.Category) FROM dbo.Products p;"); | |
CreateIndex("dbo.Products", "CategoryId"); | |
AddForeignKey("dbo.Products", "CategoryId", "dbo.Categories", "Id", cascadeDelete: true); | |
DropColumn("dbo.Products", "Category"); | |
} | |
public override void Down() | |
{ | |
AddColumn("dbo.Products", "Category", c => c.String()); | |
Sql("UPDATE p SET p.Category = (SELECT c.Name FROM dbo.Categories c WHERE c.Id = p.CategoryId) FROM dbo.Products p;"); | |
DropForeignKey("dbo.Products", "CategoryId", "dbo.Categories"); | |
DropIndex("dbo.Products", new[] { "CategoryId" }); | |
DropColumn("dbo.Products", "CategoryId"); | |
DropTable("dbo.Categories"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class AddProduct : DbMigration | |
{ | |
public override void Up() | |
{ | |
CreateTable( | |
"dbo.Products", | |
c => new | |
{ | |
Id = c.Int(nullable: false, identity: true), | |
Name = c.String(), | |
Category = c.String(), | |
}) | |
.PrimaryKey(t => t.Id); | |
Sql("INSERT INTO dbo.Products VALUES ('Product 1.1', 'Category 1');"); | |
Sql("INSERT INTO dbo.Products VALUES ('Product 1.2', 'Category 1');"); | |
Sql("INSERT INTO dbo.Products VALUES ('Product 1.3', 'Category 1');"); | |
Sql("INSERT INTO dbo.Products VALUES ('Product 2.1', 'Category 2');"); | |
Sql("INSERT INTO dbo.Products VALUES ('Product 2.2', 'Category 2');"); | |
} | |
public override void Down() | |
{ | |
DropTable("dbo.Products"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Category | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public virtual ICollection<Product> Products { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EFMigrationExample.Migrator.exe -c Server=.\sqlexpress;Database=EFMigrationExample;Trusted_Connection=True; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MigrationLogger : System.Data.Entity.Migrations.Infrastructure.MigrationsLogger | |
{ | |
private static NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger(); | |
public override void Info(string message) | |
{ | |
_logger.Info(message); | |
} | |
public override void Warning(string message) | |
{ | |
_logger.Warn(message); | |
} | |
public override void Verbose(string message) | |
{ | |
_logger.Trace(message); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Options | |
{ | |
[Option('c', "connectionString", Required = true, HelpText = "The connection string to database that needs to be updated.")] | |
public string ConnectionString { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Product | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public string Category { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Product | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public int CategoryId { get; set; } | |
public virtual Category Category { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var result = Parser.Default.ParseArguments<Options>(args); | |
result | |
.WithParsed(r => Migrate(r)); | |
} | |
private static void Migrate(Options options) | |
{ | |
var configuration = new Configuration();//Klasa Configuration z projektu DataAccess z konfiguracją migracji | |
configuration.TargetDatabase = new DbConnectionInfo( | |
options.ConnectionString, | |
"System.Data.SqlClient"); | |
var migrator = new DbMigrator(configuration); | |
MigratorLoggingDecorator logger = new MigratorLoggingDecorator(migrator, new MigrationLogger()); | |
logger.Update(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<entityFramework> | |
<contexts> | |
<context type="EFMigrationExample.DataAccess.DataContext, EFMigrationExample.DataAccess"> | |
<databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[EFMigrationExample.DataAccess.DataContext, EFMigrationExample.DataAccess], [EFMigrationExample.DataAccess.Migrations.Configuration, EFMigrationExample.DataAccess]], EntityFramework" /> | |
</context> | |
</contexts> | |
</entityFramework> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment