ComBoost項目地址
https://github.com/Kation/ComBoost/tree/develop
准備工作
首先,在Visual Studio中創建Mvc4項目。

然后使用NuGet安裝ComBoost程序包。

編寫實體
在Models文件夾里添加EmployeeGroup員工組類。
EmployeeGroup繼承EntityBase。
[DisplayName("員工組")] [DisplayColumn("GroupName", "GroupName")] public class EmployeeGroup : EntityBase { [Required] [Display(Name = "員工組名稱", Order = 0)] public virtual string GroupName { get; set; } [Hide] public virtual ICollection<Employee> Employees { get; set; } }
在Models文件夾里添加Employee員工組類。
Employee同樣繼承EntityBase。
[DisplayName("員工")] [DisplayColumn("Name", "Name")] public class Employee : EntityBase { [Display(Name = "員工名稱", Order = 0)] [Required] public virtual string Name { get; set; } [Display(Name = "性別", Order = 10)] [CustomDataType(CustomDataType.Sex)] public virtual bool Sex { get; set; } [Required] [Display(Name = "員工工號", Order = 20)] public virtual string JobNumber { get; set; } [Column(TypeName = "datetime2")] [Display(Name = "出生日期", Order = 30)] public virtual DateTime Birth { get; set; } [Display(Name = "婚否", Order = 40)] public virtual bool Marital { get; set; } [Required] [Display(Name = "部門", Order = 50)] public virtual EmployeeGroup Group { get; set; } [Display(Name = "聯系電話", Order = 60)] public virtual string Tel { get; set; } [Display(Name = "電子郵件", Order = 70)] public virtual string Email { get; set; } [Display(Name = "QQ", Order = 80)] public virtual string QQ { get; set; } }
使用EntityFramework
在NuGet里安裝EntityFramework。

安裝好后在Models文件夾添加DataContext類。
DataContext繼承EntityFramework的DbContext類。
public class DataContext : DbContext { public DbSet<Employee> Employee { get; set; } public DbSet<EmployeeGroup> EmployeeGroup { get; set; } }
之后在項目根目錄下的web.config文件里添加數據連接字符串。
<connectionStrings> <add name="DataContext" connectionString="server=127.0.0.1;database=Test;Uid=sa;Pwd=123@abc;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" /> </connectionStrings>
最后
在NuGet里安裝ComBoost.Mvc和ComBoost.Unity、ComBoost.Bootstrap程序包。

推薦更新所有程序包至最新!
修改App_Start文件夾里的UnityControllerFactory.cs
依賴注入DataContext並注冊實體控制器。
public UnityControllerFactory(IUnityContainer container) { _container = container; //Change EntityContextBuilder to your class that inherit IEntityContextBuilder interface. //If your entity context builder has constructor with arguments, continue register types that you need. container.RegisterType<DbContext, DataContext>(new MvcLifetimeManager()); container.RegisterType<IEntityContextBuilder, EntityContextBuilder>(new MvcLifetimeManager()); //Register your entity here: //RegisterController<EntityType>(); //... RegisterController<Employee>(); RegisterController<EmployeeGroup>(); }
完成
現在,您已經創建好項目了,您可以訪問/Employee或/EmployeeGroup地址對實體進行編輯。






這是個最基礎的示例,實際情況您需要對
_Layout.cshtml進行編輯,更有可能創建控制器實現其它功能,詳情請看相關文檔。
示例項目文件下載:MvcSample.rar
