热门标签 | 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



推荐阅读
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • IB 物理真题解析:比潜热、理想气体的应用
    本文是对2017年IB物理试卷paper 2中一道涉及比潜热、理想气体和功率的大题进行解析。题目涉及液氧蒸发成氧气的过程,讲解了液氧和氧气分子的结构以及蒸发后分子之间的作用力变化。同时,文章也给出了解题技巧,建议根据得分点的数量来合理分配答题时间。最后,文章提供了答案解析,标注了每个得分点的位置。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
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社区 版权所有