热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#_MVC_Repository_CRUD_Model

usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations;namespaceiFlyt

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;namespace iFlytekDemo.Models
{///

/// 城市实体/// public class City{/// /// 城市编号/// [Key]public int CityID { get; set; }/// /// 城市名称/// [Required]public string CityName { get; set; }/// /// 员工集合/// public virtual ICollection 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 All{get { return context.Cities; }}public IQueryable AllIncluding(params Expressionobject>>[] includeProperties){IQueryable 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 entitycontext.Cities.Add(city);} else {// Existing entitycontext.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 All { get; }IQueryable AllIncluding(params Expressionobject>>[] includeProperties);City Find(int id);void InsertOrUpdate(City city);void Delete(int id);void Save();}
}

 

using System.ComponentModel.DataAnnotations;namespace iFlytekDemo.Models
{///

/// 员工实体/// public class Employee{/// /// 员工编号/// [Key]public int EmployeeID { get; set; }/// /// 员工姓名/// [Required]public string EmployeeName { get; set; }/// /// 城市编号/// [Required]public int CityID { get; set; }/// /// 城市对象/// 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 All{get { return context.Employees; }}public IQueryable AllIncluding(params Expressionobject>>[] includeProperties){IQueryable 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 entitycontext.Employees.Add(employee);} else {// Existing entitycontext.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 All { get; }IQueryable AllIncluding(params Expressionobject>>[] 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());public DbSet Cities { get; set; }public DbSet Employees { get; set; }}
}



转:https://www.cnblogs.com/MarchThree/p/3720406.html



推荐阅读
author-avatar
__wolf狼
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有