====== Orchard Custom Data Access Template ======
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 =====
Yes, you need the 'Record' suffix.
namespace Module.Name.Models {
public class CustomerRecord {
public virtual int Id { get; set; }
public virtual ICollection Orders { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
}
public class OrderRecord {
public virtual int Id { get; set; }
[JsonIgnore] // Required if returning this from a Web API call.
public virtual CustomerRecord Customer { get; set; }
public virtual ICollection OrderItems { get; set; }
public virtual DateTime PlacedDate { get; set; }
public virtual DateTime SentDate { get; set; }
public virtual string Notes { get; set; }
}
public class OrderItemRecord {
public virtual int Id { get; set; }
[JsonIgnore] // Required if returning this from a Web API call.
public virtual OrderRecord OrderRecord { get; set; }
public virtual string Description { get; set; }
public virtual int Quantity { get; set; }
public virtual decimal UnitCost { get; set; }
}
}
===== 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?
namespace Module.Name {
public class DatabaseMigrations : DataMigrationImpl {
public int Create() {
SchemaBuilder.CreateTable(typeof(CustomerRecord).Name,
table => table
.Column("Id", column => column.PrimaryKey().Identity())
.Column("Name") // Will use nvarchar(255)
.Column("Description", column => column.Unlimited()) // Will use nvarchar(max)
);
SchemaBuilder.CreateTable(typeof(OrderRecord).Name,
table => table
.Column("Id", column => column.PrimaryKey().Identity())
.Column("CustomerRecord_Id")
.Column("PlacedDate")
.Column("SentDate")
.Column("Notes", column => column.Unlimited())
);
SchemaBuilder.CreateTable(typeof(OrderItemRecord).Name,
table => table
.Column("Id", column => column.PrimaryKey().Identity())
.Column("OrderRecord_Id")
.Column("Description", column => column.Unlimited())
.Column("Quantity")
.Column("UnitCost")
);
return 1;
}
}
}