· 8 years ago · Dec 16, 2017, 12:08 AM
1using Nwazet.Commerce.Models;
2using Orchard.ContentManagement.MetaData;
3using Orchard.Data;
4using Orchard.Data.Migration;
5using Orchard.Environment.Extensions;
6using System;
7using System.Data;
8
9namespace Nwazet.Commerce.Migrations {
10 [OrchardFeature("Nwazet.BaseTaxImplementations")]
11 public class BaseTaxImplementationsMigrations : DataMigrationImpl {
12
13 private readonly ITransactionManager _transactionManager;
14 private readonly IRepository<StateOrCountryTaxPartRecord> _repository;
15 public BaseTaxImplementationsMigrations(
16 ITransactionManager transactionManager,
17 IRepository<StateOrCountryTaxPartRecord> repository) {
18
19 _transactionManager = transactionManager;
20 _repository = repository;
21 }
22
23 public int Create() {
24 // we could be calling this because we enabled the feature in the migrations for
25 // Nwazet.Taxes. In that case, we do not have to actually create the tables, because
26 // those were handled there in the past.
27
28 try {
29
30 SchemaBuilder.CreateTable("StateOrCountryTaxPartRecord", table => table
31 .ContentPartRecord()
32 .Column<string>("State")
33 .Column<string>("Country")
34 .Column<decimal>("Rate")
35 .Column<int>("Priority")
36 );
37
38 ContentDefinitionManager.AlterTypeDefinition("StateOrCountryTax", cfg => cfg
39 .WithPart("StateOrCountryTaxPart"));
40
41 ContentDefinitionManager.AlterTypeDefinition("ZipCodeTax", cfg => cfg
42 .WithPart("ZipCodeTaxPart"));
43 } catch (Exception) {
44 // The CreateTable throws an SqlException if the table already exists, so I thought
45 // this could work. However, in its own handling it attempts to do _transaction.RollBack()
46 // and that fails saying "Transaction not connected, or was disconnected".
47 }
48
49 return 1;
50 }
51
52 }
53}