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; }
}
}

最新文章

  1. SQL 去掉某字段括号中的值
  2. Mybatis-mapper-xml-基础
  3. 使用crypto模块实现md5加密功能(解决中文加密前后端不一致的问题)
  4. SVN show log failed
  5. JS中Array详细用法
  6. SpringMVC框架介绍
  7. SQLSERVER监控复制并使用数据库邮件功能发告警邮件
  8. IPC——信号
  9. Mac中Eclipse配置Maven开发环境
  10. linux ARP攻击处理
  11. JS sort()实用技巧
  12. CoinChange
  13. MD5加密,解密
  14. UltraISO PE(软碟通) V9.5.5.2960 官方中文版
  15. Android JNI 使用的数据结构JNINativeMethod详解
  16. STREAMING HIVE流过滤 官网例子 注意中间用的py脚本
  17. centos 7.4 安装gitlab
  18. 细说REST API安全之概述
  19. [转] webpack之plugin内部运行机制
  20. python-day73--django课上项目01

热门文章

  1. Java [leetcode 36]Valid Sudoku
  2. RHCS集群
  3. 精简版、GHOST版win7,arduino驱动安装失败的解决方法分享
  4. Wiz开发 定时器的使用与处理
  5. 学习面试题Day04
  6. 【JMeter】JMeter在linux下运行
  7. Loadrunner通过sitescope监控mysql
  8. 【转】vnc centos
  9. 环境监测小助手V1.1的Windows版
  10. bzoj 3123 [Sdoi2013]森林(主席树,lca,启发式合并)