• 6 地址管理 D


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace LC.DAL
    {
    using Entity;
    public class AddressesDAL
    {
    /// <summary>
    /// 新增
    /// </summary>
    /// <param name="a"></param>
    /// <returns></returns>
    public int Add(Addresses a)
    {
    using (EFDbContext dbContext = new EFDbContext())
    {
    dbContext.Entry(a).State = System.Data.Entity.EntityState.Added;
    var result = dbContext.SaveChanges();
    return result;
    }
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="a"></param>
    /// <returns></returns>
    public int Update(Addresses a)
    {
    using (EFDbContext dbContext = new EFDbContext())
    {
    dbContext.Entry(a).State = System.Data.Entity.EntityState.Modified;
    var result = dbContext.SaveChanges();
    return result;
    }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="Id"></param>
    /// <returns></returns>
    public int Delete(int Id)
    {
    using (EFDbContext dbContext = new EFDbContext())
    {
    var a = dbContext.Addresses.Find(Id);
    dbContext.Entry(a).State = System.Data.Entity.EntityState.Deleted;
    var result = dbContext.SaveChanges();
    return result;
    }
    }

    /// <summary>
    /// 根据Id获取
    /// </summary>
    /// <param name="Id"></param>
    /// <returns></returns>
    public Addresses QueryById(int Id)
    {
    using (EFDbContext dbContext = new EFDbContext())
    {
    var result = dbContext.Addresses.Find(Id);
    return result;
    }
    }

    /// <summary>
    /// 获取数据
    /// </summary>
    /// <returns></returns>
    public List<Addresses> Query()
    {
    using (EFDbContext dbContext = new EFDbContext())
    {
    var linq = from a in dbContext.Addresses.ToList()
    join
    a1 in dbContext.Region.ToList() on
    a.TIdOne equals a1.Id
    join
    a2 in dbContext.Region.ToList() on
    a.TIdTwo equals a2.Id
    join
    a3 in dbContext.Region.ToList() on
    a.TIdThree equals a3.Id
    select new Addresses
    {
    Id=a.Id,
    Name=a.Name,
    Phone=a.Phone,
    Remark=a.Remark,
    TIdOne=a.TIdOne,
    TIdOneName=a1.Name,
    TIdTwo=a.TIdTwo,
    TIdTwoName=a2.Name,
    TIdThree=a.TIdThree,
    TIdThreeName=a3.Name
    };
    return linq.ToList();
    }
    }

    /// <summary>
    /// 获取数据
    /// </summary>
    /// <returns></returns>
    public List<Region> GetRegion(int PId)
    {
    using (EFDbContext dbContext = new EFDbContext())
    {
    var result = dbContext.Region.Where(m=>m.Parent_id.Equals(PId)).ToList();
    return result;
    }
    }
    }
    }

    EFDBContext

    namespace LC.DAL
    {
    using LC.Entity;
    using System;
    using System.Data.Entity;
    using System.Linq;

    public class EFDbContext : DbContext
    {
    //您的上下文已配置为从您的应用程序的配置文件(App.config 或 Web.config)
    //使用“EFDbContext”连接字符串。默认情况下,此连接字符串针对您的 LocalDb 实例上的
    //“LC.DAL.EFDbContext”数据库。
    //
    //如果您想要针对其他数据库和/或数据库提供程序,请在应用程序配置文件中修改“EFDbContext”
    //连接字符串。
    public EFDbContext()
    : base("name=EFDbContext1")
    {
    }

    //为您要在模型中包含的每种实体类型都添加 DbSet。有关配置和使用 Code First 模型
    //的详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=390109。

    public DbSet<Addresses> Addresses { get; set; }
    public DbSet<Region> Region { get; set; }
    }

    //public class MyEntity
    //{
    // public int Id { get; set; }
    // public string Name { get; set; }
    //}
    }

    model

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace LC.Entity
    {
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    public class Addresses
    {
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Phone { get; set; }
    public int TIdOne { get; set; }
    public int TIdTwo { get; set; }
    public int TIdThree { get; set; }
    public string Remark { get; set; }
    [NotMapped]
    public string TIdOneName { get; set; }
    [NotMapped]
    public string TIdTwoName { get; set; }
    [NotMapped]
    public string TIdThreeName { get; set; }
    }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace LC.Entity
    {
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    [Table("Region")]
    public class Region
    {
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public int Parent_id { get; set; }
    }
    }

  • 相关阅读:
    Linux 配置中文环境
    CookieContainer 丢失Cookie
    h5调用摄像头
    网络编程之Reactor 模式
    图形化命令行工具:
    关于VSTO调用Excel后进程无法退出的解决方案:
    ActionLink()与jquery更好地结合建造MVC网页:
    实现一个特殊栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作
    用数组结构实现大小固定的栈和队列
    比较器整理
  • 原文地址:https://www.cnblogs.com/hianb/p/10145730.html
Copyright © 2020-2023  润新知