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的更多相关文章
随机推荐
- NK 1137: 石子合并问题
1137: 石子合并问题 Time Limit: 1500 ms Memory Limit: 10000 kB Judge type: Multi-cases Total Submit ...
- 使用SharePoint 2010的母版页
转:http://tanyanbo2.blog.163.com/blog/static/97339159201111591458902/ SharePoint 2010母版页所用的还是ASP.NET ...
- java产生随机数的几种方式(转)
一.在j2se里我们可以使用Math.random()方法来产生一个随机数,这个产生的随机数是0-1之间的一个double,我们可以把他乘以一定的数,比如说乘以100,他就是个100以内的随机,这个在 ...
- [转] 三种Python下载url并保存文件的代码
原文 三种Python下载url并保存文件的代码 利用程序自己编写下载文件挺有意思的. Python中最流行的方法就是通过Http利用urllib或者urllib2模块. 当然你也可以利用ftplib ...
- Zabbix探索:网络设备监控2
在实现第一部分的简单监控的时候,在设置数据类型的时候设置成为了整数,结果: icmpping:这个没问题,只有0和1: icmppingloss:这个有问题,是百分比,其实是浮点数,单位是%: icm ...
- 《转》高级Unix命令
原文链接:http://coolshell.cn/articles/1044.html 在Unix操作中有太多太多的命令,这些命令的强大之处就是一个命令只干一件事,并把这件事干好.Do one thi ...
- Linux shell命令
一.删除监听指定端口的进程: lsof -ti: 80 | xargs kill -9 -t: 输出pid -i:查看指定端口占用情况 二.查看可执行文件动态链接库相关信息 ldd <可执行文件 ...
- 【原】Storm调度器
Storm入门教程 1. Storm基础 Storm Storm主要特点 Storm基本概念 Storm调度器 Pluggable scheduler(可插拔调度器) Isolation schedu ...
- [tensorflow in a nutshell] tensorflow简明教程 (第一部分)
原文链接: https://medium.com/@camrongodbout/tensorflow-in-a-nutshell-part-one-basics-3f4403709c9d#.31jv5 ...
- python def说明
可以这样讲,def定义了一个模块的变量,或者说是类的变量.它本身是一个函数对象.属于对象的函数,就是对象的属性.当然,你也可以叫它“方法”. python 的函数和其他语言的函数有很大区别.它是可以被 ...