orchard_custom_data_access_template

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
orchard_custom_data_access_template [2014/12/18 21:23] – created stephenorchard_custom_data_access_template [2017/01/01 20:05] (current) – external edit 127.0.0.1
Line 2: Line 2:
  
 Assuming the module = Module.Name Assuming the module = Module.Name
 +
 +Notes:
 +
 +  - Don't use a SQL reserved word for any of the columns names, e.g. 'Order'.
  
 ===== Create the models ===== ===== Create the models =====
Line 8: Line 12:
  
 <code c#> <code c#>
-namespace Module.Name.Models +namespace Module.Name.Models { 
-+    public class CustomerRecord {
-    public class CustomerRecord +
-    {+
         public virtual int Id { get; set; }         public virtual int Id { get; set; }
  
Line 20: Line 22:
     }     }
          
-    public class OrderRecord +    public class OrderRecord {
-    {+
         public virtual int Id { get; set; }         public virtual int Id { get; set; }
  
Line 34: Line 35:
     }     }
  
-    public class OrderItemRecord +    public class OrderItemRecord {
-    {+
         public virtual int Id { get; set; }         public virtual int Id { get; set; }
  
Line 47: Line 47:
 } }
 </code> </code>
 +
 +===== Create the migration =====
 +
 +Notes:
 +
 +  * Yes, you **have** to use the '_Id' suffix.
 +  * To determine the current migration number is, run ''SELECT * FROM Orchard_Framework_DataMigrationRecord''.
 +  * TODO: Why no foreign keys?
 +
 +<code c#>
 +namespace Module.Name {
 +    public class DatabaseMigrations : DataMigrationImpl {
 +
 +        public int Create() {
 +            SchemaBuilder.CreateTable(typeof(CustomerRecord).Name,
 +                table => table
 +                    .Column<int>("Id", column => column.PrimaryKey().Identity())
 +                    .Column<string>("Name"                                     // Will use nvarchar(255)
 +                    .Column<string>("Description", column => column.Unlimited()) // Will use nvarchar(max)
 +                );
 +
 +            SchemaBuilder.CreateTable(typeof(OrderRecord).Name,
 +                table => table
 +                    .Column<int>("Id", column => column.PrimaryKey().Identity())
 +                    .Column<int>("CustomerRecord_Id")
 +                    .Column<DateTime>("PlacedDate")
 +                    .Column<DateTime>("SentDate")
 +                    .Column<string>("Notes", column => column.Unlimited())
 +                );
 +                
 +            SchemaBuilder.CreateTable(typeof(OrderItemRecord).Name,
 +                table => table
 +                    .Column<int>("Id", column => column.PrimaryKey().Identity())
 +                    .Column<int>("OrderRecord_Id")
 +                    .Column<string>("Description", column => column.Unlimited())
 +                    .Column<int>("Quantity")
 +                    .Column<decimal>("UnitCost")
 +                );
 +
 +            return 1;
 +        }
 +    }
 +}
 +</code>
 +
  
orchard_custom_data_access_template.1418937833.txt.gz · Last modified: 2017/01/01 19:50 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki