Generated Classes
Generated Classes
Summary== SubSonic generates classes based on your table structure - one class per table. These classes will vary depending on which version of SubSonic you use. Avoid using the following names for table columns in your database - "SchemaName" , "ClassName", which will result in compilation errors during class generation. ==Active Record
If you're using ; } public static TableSchema.Table Schema { get { if (BaseSchema
null) SetSQLProps(); return BaseSchema; } } private static void GetTableSchema() { if(!IsSchemaInitialized) { //Schema declaration TableSchema.Table schema = new TableSchema.Table("Region", TableType.Table, DataService.GetInstance("Northwind")); schema.Columns = new TableSchema.TableColumnCollection(); schema.SchemaName = @"dbo"; //columns TableSchema.TableColumn colvarRegionID = new TableSchema.TableColumn(schema); colvarRegionID.ColumnName = "RegionID"; colvarRegionID.DataType = DbType.Int32; colvarRegionID.MaxLength = 0; colvarRegionID.AutoIncrement = true; colvarRegionID.IsNullable = false; colvarRegionID.IsPrimaryKey = true; colvarRegionID.IsForeignKey = false; colvarRegionID.IsReadOnly = false; colvarRegionID.DefaultSetting = @""; colvarRegionID.ForeignKeyTableName = ""; schema.Columns.Add(colvarRegionID); TableSchema.TableColumn colvarRegionDescription = new TableSchema.TableColumn(schema); colvarRegionDescription.ColumnName = "RegionDescription"; colvarRegionDescription.DataType = DbType.String; colvarRegionDescription.MaxLength = 50; colvarRegionDescription.AutoIncrement = false; colvarRegionDescription.IsNullable = false; colvarRegionDescription.IsPrimaryKey = false; colvarRegionDescription.IsForeignKey = false; colvarRegionDescription.IsReadOnly = false; colvarRegionDescription.DefaultSetting = @""; colvarRegionDescription.ForeignKeyTableName = ""; schema.Columns.Add(colvarRegionDescription); BaseSchema = schema; //add this schema to the provider //so we can query it later DataService.Providers["Northwind"].AddSchema("Region",schema); } } #endregion #region Props [XmlAttribute("RegionID")] [Bindable(true)] public int RegionID { get { return GetColumnValue(Columns.RegionID); } set { SetColumnValue(Columns.RegionID, value); } } [XmlAttribute("RegionDescription")] [Bindable(true)] public string RegionDescription { get { return GetColumnValue(Columns.RegionDescription); } set { SetColumnValue(Columns.RegionDescription, value); } } #endregion #region PrimaryKey Methods protected override void SetPrimaryKey(object oValue) { base.SetPrimaryKey(oValue); SetPKValues(); } public Northwind.TerritoryCollection Territories() { return new Northwind.TerritoryCollection() .Where(Territory.Columns.RegionID, RegionID).Load(); } #endregion //no foreign key tables defined (0) //no ManyToMany tables defined (0) #region ObjectDataSource support /// /// Inserts a record, can be used with the Object Data Source /// public static void Insert(string varRegionDescription) { Region item = new Region(); item.RegionDescription = varRegionDescription; if (System.Web.HttpContext.Current != null) item.Save(System.Web.HttpContext.Current.User.Identity.Name); else item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name); } /// /// Updates a record, can be used with the Object Data Source /// public static void Update(int varRegionID,string varRegionDescription) { Region item = new Region(); item.RegionID = varRegionID; item.RegionDescription = varRegionDescription; item.IsNew = false; if (System.Web.HttpContext.Current != null) item.Save(System.Web.HttpContext.Current.User.Identity.Name); else item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name); } #endregion #region Typed Columns public static TableSchema.TableColumn RegionIDColumn { get { return Schema.Columns[0]; } } public static TableSchema.TableColumn RegionDescriptionColumn { get { return Schema.Columns[1]; } } #endregion #region Columns Struct public struct Columns { public static string RegionID = @"RegionID"; public static string RegionDescription = @"RegionDescription"; } #endregion #region Update PK Collections public void SetPKValues() { } #endregion #region Deep Save public void DeepSave() { Save(); } #endregion } } ==Active Record Foreign Keys== All foreign key tables are attached to these classes as child collections which you can query and access as needed. ==Customizing Active Record
You can change the code generated here by tweaking the code templates used to produce the classes. This code is found in your SubSonic install folder, inside the "Templates" directory. By default you can just place this file into your project in the "CodeTemplates" directory and SubSonic will find it while generating your code. You can override this setting by telling SubSonic (2.x only) where the templates are:
POCOs With SubSonic 3
As of 3.0, our preference is to get away from inheritance constraints which decrease SubSonic's flexibility and to go with "Plain Old CLR Objects" - or "POCOs" as they are called. This is to facilitate testability, which is core to ASP.NET MVC. The Northwind Regions table, therefore, would generate this code (a sample template - yours may be different):
///
Customizing 3.0 POCOs
All classes are created using our Classes.tt file, which is a Visual Studio code-generation file. You can change this code to reflect whatever it is you need to do. And if you do something brilliant you can !