C#_MVC_Repository_CRUD_Model
2024-09-01 13:40:28
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; }
}
}
最新文章
- NodeJS中的异步I/O、事件驱动
- codeforces575A Fibonotci
- mysql 字段编码该为utf8mb4
- 高新服务平台在SUSE系统上重新部署笔录
- [转]UINavigationController 返回的方法汇总
- C语言里的指针探析——type *name[] 在函数参数里面,是一个二维指针
- 【转载】C# HttpWebRequest 发送SOAP XML
- leetcode面试准备:Decode Ways
- 解决Button在IE6、7下的自适应宽度问题
- ListView数据动态刷新
- php钩子程序设计
- phpstrom识别phalcon框架模板文件的配置
- Unreachable statement
- python 爬虫之beautifulsoup(bs4)环境准备
- 如何验证代理ip的正确性
- Go斐波拉契数列(Fibonacci)(多种写法)
- 深入理解JVM虚拟机:(一)Java运行时数据区域
- CQOI2018 简要题解
- 使用Visual Studio Team Services进行压力和性能测试(二)——压力测试执行
- QA CodeDiff做什么?什么时间做?
热门文章
- Java [leetcode 29]Divide Two Integers
- HDU 5688 Problem D
- HDU 1043 Eight BFS
- ASP.NET使用Jquery-Ajax向ashx传递参数中文出现乱码
- 【译】 AWK教程指南 8处理多行数据
- JAVA——装箱和拆箱
- Java反射机制练习(增强可扩展性)
- Property cannot be found on forward class object?
- Android项目实战--手机卫士24--程序锁的实现以及逻辑
- Remastersys打包你自己的ubuntu成iso文件,保存原来的所有配置