Asp.Net Mvc + ComBoost.Mvc快速开发
2015-09-25 09:52
519 查看
ComBoost项目地址
http://comboost.wodsoft.comhttps://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
相关文章推荐
- ASP.NETWeb服务器验证控件如何使用
- ASP.NET预备知识学习笔记
- asp.net 事件加载顺序
- asp.net中SQL语句太长,怎么换行写?
- ASPx label显示更多文字的滚动条
- 第三章 AOP 基于@AspectJ的AOP
- AspNet MVC过滤器+实例
- 实战:把ASP.NET MVC中的Views下面的视图放到Views文件夹外
- WCF The service cannot be activated because it does not support ASP.NET compatibility
- WCF The service cannot be activated because it does not support ASP.NET compatibility
- Installing Metasploit Framework on Ubuntu 14.04 LTS and Debian 7
- RaspberryPi 更新国内源
- Web.config配置文件
- ASP.NET中事件如何依次发生?
- vs2010制作简单的asp.net网站
- ASP实现文件上传的方法
- asp循环语句总结
- ASP 数字分页效果代码分享
- [ASP.NET]分析MVC5源码,并实现一个ASP.MVC
- 浅析Asp.net MVC 中Ajax的使用