zoukankan      html  css  js  c++  java
  • C#_MVC_Repository_CRUD_Model

    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; }
        }
    }

  • 相关阅读:
    【修正】gooogleman嵌入式联盟部分图标,并增加gooogleman名片(20101205)
    【程序基础】==和=号的区别
    gooogleman嵌入式开发板联盟准备配发嵌入式视频教程
    【终极版】gooogleman嵌入式开发板联盟图标发布以及使用说明
    关于wince驱动和应用学习的N个问题
    【原创】深刻体会wince 驱动中Sleep函数的作用
    网站策划的经典语录
    repeater中用单选按钮
    一个专业制作网站者的自白
    (原创)文件压缩代码
  • 原文地址:https://www.cnblogs.com/MarchThree/p/3720406.html
Copyright © 2011-2022 走看看