網站首頁 編程語言 正文
有一段時間沒有更新博客了,趕上今天外面下雨,而且沒人約球,打算把最近對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<Lodging> 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:
<connectionStrings> <add name="BAContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=DataAccess.BreakAwayContext;Integrated Security=SSPI;" providerName="System.Data.SqlClient" /> </connectionStrings>
DB Context:
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using Model;
namespace DataAccess
{
public class TTableConfiguration : EntityTypeConfiguration<TTable>
{
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<Destination> Destinations { get; set; }
public DbSet<Lodging> Lodgings { get; set; }
public DbSet<TTable> TTables { get; set; }
}
}
2. 如果數據庫的表的字段和領域模型的字段不對應,如何處理呢?比如本文的TTable表是在TTT? Schema下面的, 而其他表示設計在DBO下面,最方便的方式是使用fluent API, 具體代碼如請參見?TTableConfiguration Class和?OnModelCreating()方法,可配置的粒度非常細,比如可以配置領域模型和數據庫的哪個Schema的哪張表的哪一列對應,本文是將TTable 類的數據庫表配置為了TTT? Schema下的TTable表,
public class TTableConfiguration : EntityTypeConfiguration<TTable>
{
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<Destination> 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<Destination> QueryPaging<TKey>(int pageIndex, int pageSize, Expression<Func<Destination, bool>> whereLambda, Expression<Func<Destination, TKey>> 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進行增刪改查操作給出了詳盡的解釋,并且給出了數據庫和領域模型代碼分開設計的完整解決方案,同時介紹了手動數據庫表和領域模型映射,數據庫事務實現,分頁查詢等常用功能,希望對大家有所幫助。
原文鏈接:https://www.cnblogs.com/pugang/p/8909414.html
相關推薦
- 2023-02-09 詳解如何使用Python實現復制粘貼的功能_python
- 2022-07-02 Python?matplotlib繪圖時使用鼠標滾輪放大/縮小圖像_python
- 2022-11-06 React中useEffect與生命周期鉤子函數的對應關系說明_React
- 2022-06-30 Oracle在PL/SQL中使用子查詢_oracle
- 2022-08-21 Go語言的互斥鎖的詳細使用_Golang
- 2023-10-12 利用touch-action解決驗證碼滑塊滑動時,背景跟隨一起滑動的問題,以及詳解touch-act
- 2022-12-29 解決React報錯Property?value?does?not?exist?on?type?HTM
- 2022-05-17 docker停止某個容器
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支