C#_MVC_Repository_CRUD_Model
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; namespace iFlytekDemo.Models
{
/// <summary>
/// 城市实体
/// </summary>
public class City
{
/// <summary>
/// 城市编号
/// </summary>
[Key]
public int CityID { get; set; } /// <summary>
/// 城市名称
/// </summary>
[Required]
public string CityName { get; set; } /// <summary>
/// 员工集合
/// </summary>
public virtual ICollection<Employee> Employees { get; set; }
}
}


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web; namespace iFlytekDemo.Models
{
public class CityRepository : ICityRepository
{
iFlytekDemoContext context = new iFlytekDemoContext(); public IQueryable<City> All
{
get { return context.Cities; }
} public IQueryable<City> AllIncluding(params Expression<Func<City, object>>[] includeProperties)
{
IQueryable<City> query = context.Cities;
foreach (var includeProperty in includeProperties) {
query = query.Include(includeProperty);
}
return query;
} public City Find(int id)
{
return context.Cities.Find(id);
} public void InsertOrUpdate(City city)
{
if (city.CityID == default(int)) {
// New entity
context.Cities.Add(city);
} else {
// Existing entity
context.Entry(city).State = EntityState.Modified;
}
} public void Delete(int id)
{
var city = context.Cities.Find(id);
context.Cities.Remove(city);
} public void Save()
{
context.SaveChanges();
} public void Dispose()
{
context.Dispose();
}
} public interface ICityRepository : IDisposable
{
IQueryable<City> All { get; }
IQueryable<City> AllIncluding(params Expression<Func<City, object>>[] includeProperties);
City Find(int id);
void InsertOrUpdate(City city);
void Delete(int id);
void Save();
}
}


using System.ComponentModel.DataAnnotations; namespace iFlytekDemo.Models
{
/// <summary>
/// 员工实体
/// </summary>
public class Employee
{
/// <summary>
/// 员工编号
/// </summary>
[Key]
public int EmployeeID { get; set; } /// <summary>
/// 员工姓名
/// </summary>
[Required]
public string EmployeeName { get; set; } /// <summary>
/// 城市编号
/// </summary>
[Required]
public int CityID { get; set; } /// <summary>
/// 城市对象
/// </summary>
public virtual City City { get; set; }
}
}


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web; namespace iFlytekDemo.Models
{
public class EmployeeRepository : IEmployeeRepository
{
iFlytekDemoContext context = new iFlytekDemoContext(); public IQueryable<Employee> All
{
get { return context.Employees; }
} public IQueryable<Employee> AllIncluding(params Expression<Func<Employee, object>>[] includeProperties)
{
IQueryable<Employee> query = context.Employees;
foreach (var includeProperty in includeProperties) {
query = query.Include(includeProperty);
}
return query;
} public Employee Find(int id)
{
return context.Employees.Find(id);
} public void InsertOrUpdate(Employee employee)
{
if (employee.EmployeeID == default(int)) {
// New entity
context.Employees.Add(employee);
} else {
// Existing entity
context.Entry(employee).State = EntityState.Modified;
}
} public void Delete(int id)
{
var employee = context.Employees.Find(id);
context.Employees.Remove(employee);
} public void Save()
{
context.SaveChanges();
} public void Dispose()
{
context.Dispose();
}
} public interface IEmployeeRepository : IDisposable
{
IQueryable<Employee> All { get; }
IQueryable<Employee> AllIncluding(params Expression<Func<Employee, object>>[] includeProperties);
Employee Find(int id);
void InsertOrUpdate(Employee employee);
void Delete(int id);
void Save();
}
}


using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web; namespace iFlytekDemo.Models
{
public class iFlytekDemoContext : DbContext
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to drop and regenerate your database
// automatically whenever you change your model schema, add the following
// code to the Application_Start method in your Global.asax file.
// Note: this will destroy and re-create your database with every model change.
//
// System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<iFlytekDemo.Models.iFlytekDemoContext>()); public DbSet<iFlytekDemo.Models.City> Cities { get; set; } public DbSet<iFlytekDemo.Models.Employee> Employees { get; set; }
}
}
C#_MVC_Repository_CRUD_Model的更多相关文章
随机推荐
- [html5] (Notification) 桌面通知
前几天要做一个桌面通知的功能,翻查以前做的笔记,发现webkitNotifications这个已经不能用了,baidu了下,基本都是介绍webkitNotifications的,后来在SOF上找到答案 ...
- Oracle 参数化更新数据时报错:Oracle ORA-01722: 无效数字
报错:Oracle ORA-01722: 无效数字 看了一篇博客,据说是参数与列名不能一致,改过之后还是报一样的错误:Oracle ORA-01722: 无效数字 ,后来试了一下,不是参数名必须不一样 ...
- 《深入Java虚拟机学习笔记》- 第10章 栈和局部变量操作
Java栈和局部变量操作 Java虚拟机是基于栈的机器,几乎所有Java虚拟机的指令都与操作数栈相关.栈操作包括把常量压入操作数栈.执行通用的栈操作.在操作数栈和局部变量之间往返传输值. 1常量入栈操 ...
- android和ios的系统特性区别
1. 删除:android是长按,ios为滑动/或者进入编辑模式 2. android:应用内返回键,保证用户停留在应用程序中:硬件返回键可以让用户退出应用程序 3. 剪切/粘贴/选择等文本操作 ...
- Visual Studio配置OpenCV设置全局的继承属性
1.安装完毕OpenCV后,新建一个CLR空项目,将其取名为"SetingGlobalOpenCVDir"便于以后变更版本时修改.如下图所示: 2.点击"视图->其 ...
- 【zoj3254】Secret Code
题意: 给出a.p.d.m 求a^x=d(mod p) 0<=x<=m 的解的个数 题解: 今天一整天的时间大部分都在调这题Orz BSGS什么的还是太不熟了 我们可以用BSGS拓展版求出 ...
- 问题-Tbutton(sender) 与 (sender as Tbutton) 等价吗?
问题:Tbutton(sender) 与 (sender as Tbutton) 等价吗? 答: 1. Sender As TButton时delphi做类型检查. 比如: var frm:TFo ...
- 【C语言】-条件语句-switch语句
switch语句: 用于直接处理不同情况下的多路问题. switch语句又可称为开关语句,其执行流程和多分支if语句类似. switch (表达式) { case 常量表达式1:语句组1;break; ...
- python 递归函数
在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 举个例子,我们来计算阶乘n! = 1 x 2 x 3 x ... x n,用函数fact(n)表示,可以看出: f ...
- ALAssetsLibrary学习总结
添加AssetsLibrary.framework 然后引入 #import <AssetsLibrary/ALAssetsLibrary.h> 一个获取所有图片的类 #import &l ...