阅读此文前请先掌握和
1、创建数据库和表
CREATE DATABASE IocProjectDBUSE [IocProjectDB]GO/****** Object: Table [dbo].[T_User] Script Date: 12/29/2017 15:49:35 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[T_User]( [UserID] [int] IDENTITY(1,1) NOT NULL, [UserCode] [nvarchar](50) NOT NULL, [UserName] [nvarchar](50) NULL, [UserPwd] [nvarchar](50) NOT NULL, [Gender] [nchar](10) NULL, [Birthday] [datetime] NULL, [MobileNo] [nvarchar](50) NULL, [Address] [nvarchar](50) NULL, [Remarks] [nvarchar](200) NULL, [CreateUserID] [int] NULL, [CreateTime] [datetime] NULL, CONSTRAINT [PK_T_User] PRIMARY KEY CLUSTERED ( [UserID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOSET IDENTITY_INSERT [dbo].[T_User] ONINSERT [dbo].[T_User] ([UserID], [UserCode], [UserName], [UserPwd], [Gender], [Birthday], [MobileNo], [Address], [Remarks], [CreateUserID], [CreateTime]) VALUES (1, N'zhangsan', N'张三', N'123', N'F ', CAST(0x000088B100000000 AS DateTime), N'15365428564', N'广东深圳福田', N'测试用户', 7, CAST(0x0000A85800000000 AS DateTime))INSERT [dbo].[T_User] ([UserID], [UserCode], [UserName], [UserPwd], [Gender], [Birthday], [MobileNo], [Address], [Remarks], [CreateUserID], [CreateTime]) VALUES (2, N'lisi', N'李四', N'456', N'M ', CAST(0x0000863100000000 AS DateTime), N'13526548954', N'湖北武汉光谷', N'测试用户', 7, CAST(0x0000A85800000000 AS DateTime))SET IDENTITY_INSERT [dbo].[T_User] OFF
2、使用EF连接数据库
1)在IocProject.Model层创建Models文件夹,然后文件夹下创建实体数据模型DbModels.edmx,具体步骤截图如下:
选择从数据库生成
创建数据库连接
选是,点击下一步,选择实体框架6.0,下一步选择要创建的表
最终效果如下图:
3、在IocProject.Dao层中连接数据库操作并实现查询父类与子类
1)在IocProject.Dao层创建一个BaseDBContext类,用来自定义EF上下文,首先引用EF框架。备注:IocProject.Dao层需要引用IocProject.Model层。
创建BaseDbContext类:
namespace IocProject.Dao{ ////// 自定义的EF上下文容器类 /// public class BaseDbContext : DbContext { ////// 负责根据指定的数据库链接字符串,初始化EF /// public BaseDbContext() : base("name=IocProjectDBEntities") { } }}
2)创建BaseDao类来存放每个类中共有的方法:查询、新增、编辑、删除、执行Sql语句。
这里先实现一个查询的方法,然后接下来在扩充,创建BaseDao类和IBaseDao接口:namespace IocProject.Dao{ ////// 负责操作数据库中的所有表的普通增,删,查,改操作的 /// ///public class BaseDao : IBaseDao where TEntity : class { //实例化EF上下文容器对象 BaseDbContext db = new BaseDbContext(); DbSet _dbset; public BaseDao() { //初始化 _dbset = db.Set (); } #region 查询 /// /// 查询所有数据 /// ///public List QueryList() { return _dbset.ToList(); } /// /// 根据条件查询单个实体数据 /// /// ///public List Query(Expression > where) { return _dbset.Where(where).ToList(); } #endregion }}
namespace IocProject.Dao{ public interface IBaseDaowhere TEntity : class { /// /// 查询所有数据 /// ///List QueryList(); /// /// 根据条件查询单个实体数据 /// /// ///List Query(Expression > where); }}
3)创建UserDao类和IUserDao接口来继承BaseDao和IBaseDao:
namespace IocProject.Dao.User{ public class UserDao : BaseDao, IUserDao { }}
namespace IocProject.Dao.User{ public interface IUserDao : IBaseDao{ }}
4、在IocProject.Service层调用IocProject.Dao层
1)创建BaseService基类和IBaseService接口,并且调用BaseDao中查询的方法
namespace IocProject.Service{ public class BaseService:IBaseService where TEntity : class { public IBaseDao _baseDao; /// /// 构造器注入 /// /// public BaseService(IBaseDaobaseDao) { _baseDao = baseDao; } #region 查询 /// /// 查询所有数据 /// ///public List QueryList() { return _baseDao.QueryList(); } /// /// 根据条件查询单个实体数据 /// /// ///public List Query(Expression > where) { return _baseDao.Query(where); } #endregion }}
namespace IocProject.Service{ public interface IBaseServicewhere TEntity : class { /// /// 查询所有数据 /// ///List QueryList(); /// /// 根据条件查询单个实体数据 /// /// ///List Query(Expression > where); }}
2) 创建UserService类和IUserService接口来继承BaseService和IBaseService:
namespace IocProject.Service.User{ public class UserService : BaseService, IUserService { public IUserDao _userDao; /// /// 构造器注入 /// /// public UserService(IUserDao userDao) : base(userDao) { _userDao = userDao; } }}
namespace IocProject.Service.User{ public interface IUserService:IBaseService{ }}
5、在IocProject.WebApi层添加控制器UserController.cs
namespace IocProject.WebApi.Controllers{ public class UserController : ApiController { public IUserService _userService; ////// 构造器注入 /// /// public UserController(IUserService userService) { _userService = userService; } ////// 查询所有数据 /// ///[HttpGet] public IEnumerable QueryList() { return _userService.QueryList(); } /// /// 根据id查询单个实体数据 /// /// ///[HttpGet] public IEnumerable Query(int id) { return _userService.Query(c => c.UserID == id); } }}
修改App_Start文件夹下UnityConfig类下的Initialise方法,如下图:
namespace IocProject.WebApi{ public static class UnityConfig { ////// 注册接口和实现 /// /// public static void Initialise(HttpConfiguration config) { var container = new UnityContainer(); container.RegisterType, BaseDao >(); container.RegisterType , BaseService >(); container.RegisterType (); container.RegisterType (); config.DependencyResolver = new UnityResolver(container); } } class TEntity { }}
然后在IocProject.Model层中的App.Config文件中把数据库链接拷贝到IocProject.WebApi层Web.config中,如下图:
6、运行项目
在浏览器Url后面加上“/api/user”查询所有的用户,加上“/api/user/2”查询单个用户,如下图: