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



推荐阅读
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • Spring框架《一》简介
    Spring框架《一》1.Spring概述1.1简介1.2Spring模板二、IOC容器和Bean1.IOC和DI简介2.三种通过类型获取bean3.给bean的属性赋值3.1依赖 ... [详细]
  • 【Mysql】九、Mysql高级篇 索引
    MYSQL索引一、什么是索引?二、索引数据结构1、mysql数据库的四种索引2、BTREE结构三、索引分类、创建索引、查看索引1、单值索引2、复合索引3、函数索引4、 ... [详细]
  • 基于PgpoolII的PostgreSQL集群安装与配置教程
    本文介绍了基于PgpoolII的PostgreSQL集群的安装与配置教程。Pgpool-II是一个位于PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件,提供了连接池、复制、负载均衡、缓存、看门狗、限制链接等功能,可以用于搭建高可用的PostgreSQL集群。文章详细介绍了通过yum安装Pgpool-II的步骤,并提供了相关的官方参考地址。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • Android日历提醒软件开源项目分享及使用教程
    本文介绍了一款名为Android日历提醒软件的开源项目,作者分享了该项目的代码和使用教程,并提供了GitHub项目地址。文章详细介绍了该软件的主界面风格、日程信息的分类查看功能,以及添加日程提醒和查看详情的界面。同时,作者还提醒了读者在使用过程中可能遇到的Android6.0权限问题,并提供了解决方法。 ... [详细]
  • 本文讨论了在使用Git进行版本控制时,如何提供类似CVS中自动增加版本号的功能。作者介绍了Git中的其他版本表示方式,如git describe命令,并提供了使用这些表示方式来确定文件更新情况的示例。此外,文章还介绍了启用$Id:$功能的方法,并讨论了一些开发者在使用Git时的需求和使用场景。 ... [详细]
  • 本文详细介绍了在Linux虚拟化部署中进行VLAN配置的方法。首先要确认Linux系统内核是否已经支持VLAN功能,然后配置物理网卡、子网卡和虚拟VLAN网卡的关系。接着介绍了在Linux配置VLAN Trunk的步骤,包括将物理网卡添加到VLAN、检查添加的VLAN虚拟网卡信息以及重启网络服务等。最后,通过验证连通性来确认配置是否成功。 ... [详细]
  • Yii framwork 应用小窍门
    Yiiframework应用小窍门1.YiiFramework]如何获取当前controller的名称?下面语句就可以获取当前控制器的名称了!Php代码 ... [详细]
  • docker安装到基本使用
    记录docker概念,安装及入门日常使用Docker安装查看官方文档,在"Debian上安装Docker",其他平台在"这里查 ... [详细]
  • 前言:原本纠结于Web模板,选了Handlebars。后来发现页面都是弱逻辑的,不支持复杂逻辑表达式。几乎要放弃之际,想起了Javascript中ev ... [详细]
  • 本文整理了Java中org.assertj.core.api.AbstractPathAssert.existsNoFollowLinks()方法的一些代码示例,展示了 ... [详细]
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社区 版权所有