搜索

查看: 3068|回复: 11

[ASP.NET] Entity Framework使用DBContext实现增删改查

[复制链接]
发表于 2023-5-4 11:24:33 | 显示全部楼层 |阅读模式
Editor 2023-5-4 11:24:33 3068 11 看全部
有一段时间没有更新博客了,赶上今天外面下雨,而且没人约球,打算把最近对Entity Framework DBContext使用的心得梳理一下,早些时候在网上简单查过,对于最新版本的EF并没有类似的知识梳理类文章,希望对大家有所帮助。
1. 不要Code first, 也不要DB first
我为什么讨厌Code first和DB first呢?首先Code first是先写代码,数据库完全由代码生成,开发阶段尚可,一旦到了产品发布阶段,如果需要添加字段,我们总不能用 visual studio去生产环境上去更新数据库吧,听起来就很可怕。而且另外的一个问题自动是生成的数据库脚本也不可控,还不如自己提前设计好。DB first也好不了哪去,反向转过来的代码包含很多没有用的文件,而且数据库的更新还要重新走Model生成过程,简直无法理解为什么会有这样的设计。说了这么多,怎么解决呢?
数据库和领域模型分开设计,按照对应关系映射字段,使用自定义链接字串,既不使用领域模型生成数据库,也不用数据库生成领域模型,示例代码如下,SQL Code 以 Destinations和TTable表为例:
CREATE TABLE [DBO].[Destinations]
(
    [DestinationId] [int] PRIMARY KEY NOT NULL,
    [Name] [nvarchar](max) NULL,
    [Country] [nvarchar](max) NULL,
    [Description] [nvarchar](max) NULL,
    [Photo] [varbinary](max) NULL
CREATE TABLE [TTT].[TTable]
(
[Id] [int] PRIMARY KEY NOT NULL,
[Name] [nvarchar](max) NULL

Model Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model
{
    public class Destination
    {
        public int DestinationId { get; set; }
        public string Name { get; set; }
        public string Country { get; set; }
        public string Description { get; set; }
        public byte[] Photo { get; set; }
        public List Lodgings { get; set; }
    }
    public class Lodging
    {
        public int LodgingId { get; set; }
        public string Name { get; set; }
        public string Owner { get; set; }
        public bool IsResort { get; set; }
        public Destination Destination { get; set; }
    }
    public class TTable
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}
Connect String:

   
  
DB Context:
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using Model;
namespace DataAccess
{
    public class TTableConfiguration : EntityTypeConfiguration
    {
        public TTableConfiguration()
        {
            this.ToTable("TTable", "TTT");
        }
    }
    public class BreakAwayContext : DbContext
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new TTableConfiguration());
        }
        public BreakAwayContext(string connString) : base(connString)
        {
        }
        public DbSet Destinations { get; set; }
        public DbSet Lodgings { get; set; }
        public DbSet TTables { get; set; }
    }
}
2. 如果数据库的表的字段和领域模型的字段不对应,如何处理呢?比如本文的TTable表是在TTT  Schema下面的, 而其他表示设计在DBO下面,最方便的方式是使用fluent API, 具体代码如请参见 TTableConfiguration Class和 OnModelCreating()方法,可配置的粒度非常细,比如可以配置领域模型和数据库的哪个Schema的哪张表的哪一列对应,本文是将TTable 类的数据库表配置为了TTT  Schema下的TTable表,
    public class TTableConfiguration : EntityTypeConfiguration
    {
        public TTableConfiguration()
        {
            this.ToTable("TTable", "TTT");
        }
    }
3. 增删该查自带事物支持,具体代码如下,
        public static int Insert()
        {
            var destination = new Destination
            {
                Country = "Chs",
                Description = "Chs is the language package",
                Name = "xsss"
            };
            using (var context = new BreakAwayContext(ConfigurationManager.ConnectionStrings["BAContext"].ConnectionString))
            {
                var rt = context.Destinations.Add(destination);
                context.SaveChanges();
                return rt.DestinationId;
            }
        }
        public static void Update(Destination destIn)
        {
            using (var context = new BreakAwayContext(ConfigurationManager.ConnectionStrings["BAContext"].ConnectionString))
            {
                var dest = context.Destinations.Where(a => a.DestinationId == destIn.DestinationId).Single();
                dest.Name = destIn.Name;
                context.SaveChanges();
            }
        }
        public static void Delete(int destId)
        {
            using (var context = new BreakAwayContext(ConfigurationManager.ConnectionStrings["BAContext"].ConnectionString))
            {
                var destination = new Destination() { DestinationId = destId };
                context.Destinations.Attach(destination);
                context.Destinations.Remove(destination);
                context.SaveChanges();
            }
        }

        public static Destination Query(int destId)
        {
            using (var context = new BreakAwayContext(ConfigurationManager.ConnectionStrings["BAContext"].ConnectionString))
            {
                IQueryable dest = context.Destinations.Where(a => a.DestinationId == destId);
                return dest.Single();
            }
        }
4. 如果需要多个操作同时成功或者失败,需要手动开启事务,具体代码如下,
        public static void TransactionOps()
        {
            using (var context = new BreakAwayContext(ConfigurationManager.ConnectionStrings["BAContext"].ConnectionString))
            {
                using (var dbContextTransaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        var destination = new Destination
                        {
                            Country = "Chs",
                            Description = "Chs is the language package",
                            Name = "xs2s"
                        };
                        var destId = context.Destinations.Add(destination);
                        context.SaveChanges();
                        context.Destinations.Attach(destId);
                        context.Destinations.Remove(destId);
                        context.SaveChanges();
                        dbContextTransaction.Commit();
                    }
                    catch (System.Exception ex)
                    {
                        dbContextTransaction.Rollback();
                        System.Console.WriteLine(ex.ToString());
                    }
                }
            }
        }
5. 分页查询是网站设计的常用功能,一个简单的真分页查询方法如下如下所示,
        public static List QueryPaging(int pageIndex, int pageSize, Expression> whereLambda, Expression> orderBy)
        {
            using (var context = new BreakAwayContext(ConfigurationManager.ConnectionStrings["BAContext"].ConnectionString))
            {
                return context.Destinations.Where(whereLambda).OrderBy(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            }
        }
总结
本文对最新版本的Entity Framework进行增删改查操作给出了详尽的解释,并且给出了数据库和领域模型代码分开设计的完整解决方案,同时介绍了手动数据库表和领域模型映射,数据库事务实现,分页查询等常用功能,希望对大家有所帮助。
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对知鸟论坛的支持。如果你想了解更多相关内容请查看下面相关链接
回复

使用道具 举报

发表于 2023-6-29 04:12:30 | 显示全部楼层
Gordon520 2023-6-29 04:12:30 看全部
我看不错噢 谢谢楼主!知鸟论坛越来越好!
回复

使用道具 举报

发表于 2023-6-29 04:49:46 | 显示全部楼层
知足常乐77 2023-6-29 04:49:46 看全部
我看不错噢 谢谢楼主!知鸟论坛越来越好!
回复

使用道具 举报

发表于 2023-6-29 16:59:59 | 显示全部楼层
尘埃416 2023-6-29 16:59:59 看全部
既然你诚信诚意的推荐了,那我就勉为其难的看看吧!知鸟论坛不走平凡路。
回复

使用道具 举报

发表于 2023-6-29 18:38:49 | 显示全部楼层
墙和鸡蛋 2023-6-29 18:38:49 看全部
既然你诚信诚意的推荐了,那我就勉为其难的看看吧!知鸟论坛不走平凡路。
回复

使用道具 举报

发表于 2023-6-29 21:24:10 | 显示全部楼层
十二音阶囤 2023-6-29 21:24:10 看全部
感谢楼主的无私分享!要想知鸟论坛好 就靠你我他
回复

使用道具 举报

发表于 2023-6-29 23:15:19 | 显示全部楼层
伊索谗言 2023-6-29 23:15:19 看全部
楼主,大恩不言谢了!知鸟论坛是最棒的!
回复

使用道具 举报

发表于 2023-6-29 23:53:04 | 显示全部楼层
心随674 2023-6-29 23:53:04 看全部
这东西我收了!谢谢楼主!知鸟论坛真好!
回复

使用道具 举报

发表于 2023-6-30 10:57:03 | 显示全部楼层
米老鼠和蓝精鼠v 2023-6-30 10:57:03 看全部
楼主,我太崇拜你了!我想我是一天也不能离开知鸟论坛
回复

使用道具 举报

发表于 2023-6-30 11:12:12 | 显示全部楼层
123456868 2023-6-30 11:12:12 看全部
楼主发贴辛苦了,谢谢楼主分享!我觉得知鸟论坛是注册对了!
回复

使用道具 举报

  • 您可能感兴趣
点击右侧快捷回复 【请勿灌水】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则 返回列表

RSS订阅| SiteMap| 小黑屋| 知鸟论坛
联系邮箱E-mail:zniao@foxmail.com
快速回复 返回顶部 返回列表