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



推荐阅读
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • 本文介绍了如何在C#中启动一个应用程序,并通过枚举窗口来获取其主窗口句柄。当使用Process类启动程序时,我们通常只能获得进程的句柄,而主窗口句柄可能为0。因此,我们需要使用API函数和回调机制来准确获取主窗口句柄。 ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • 本文探讨了如何优化和正确配置Kafka Streams应用程序以确保准确的状态存储查询。通过调整配置参数和代码逻辑,可以有效解决数据不一致的问题。 ... [详细]
  • Splay Tree 区间操作优化
    本文详细介绍了使用Splay Tree进行区间操作的实现方法,包括插入、删除、修改、翻转和求和等操作。通过这些操作,可以高效地处理动态序列问题,并且代码实现具有一定的挑战性,有助于编程能力的提升。 ... [详细]
  • 本文探讨了如何在编程中正确处理包含空数组的 JSON 对象,提供了详细的代码示例和解决方案。 ... [详细]
  • 尽管使用TensorFlow和PyTorch等成熟框架可以显著降低实现递归神经网络(RNN)的门槛,但对于初学者来说,理解其底层原理至关重要。本文将引导您使用NumPy从头构建一个用于自然语言处理(NLP)的RNN模型。 ... [详细]
  • ASP.NET MVC中Area机制的实现与优化
    本文探讨了在ASP.NET MVC框架中,如何通过Area机制有效地组织和管理大规模应用程序的不同功能模块。通过合理的文件夹结构和命名规则,开发人员可以更高效地管理和扩展项目。 ... [详细]
  • 本题通过将每个矩形视为一个节点,根据其相对位置构建拓扑图,并利用深度优先搜索(DFS)或状态压缩动态规划(DP)求解最小涂色次数。本文详细解析了该问题的建模思路与算法实现。 ... [详细]
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社区 版权所有