Entity?Framework使用DBContext實(shí)現(xiàn)增刪改查
有一段時間沒有更新博客了,趕上今天外面下雨,而且沒人約球,打算把最近對Entity Framework DBContext使用的心得梳理一下,早些時候在網(wǎng)上簡單查過,對于最新版本的EF并沒有類似的知識梳理類文章,希望對大家有所幫助。
1. 不要Code first, 也不要DB first
我為什么討厭Code first和DB first呢?首先Code first是先寫代碼,數(shù)據(jù)庫完全由代碼生成,開發(fā)階段尚可,一旦到了產(chǎn)品發(fā)布階段,如果需要添加字段,我們總不能用 visual studio去生產(chǎn)環(huán)境上去更新數(shù)據(jù)庫吧,聽起來就很可怕。而且另外的一個問題自動是生成的數(shù)據(jù)庫腳本也不可控,還不如自己提前設(shè)計(jì)好。DB first也好不了哪去,反向轉(zhuǎn)過來的代碼包含很多沒有用的文件,而且數(shù)據(jù)庫的更新還要重新走M(jìn)odel生成過程,簡直無法理解為什么會有這樣的設(shè)計(jì)。說了這么多,怎么解決呢?
數(shù)據(jù)庫和領(lǐng)域模型分開設(shè)計(jì),按照對應(yīng)關(guān)系映射字段,使用自定義鏈接字串,既不使用領(lǐng)域模型生成數(shù)據(jù)庫,也不用數(shù)據(jù)庫生成領(lǐng)域模型,示例代碼如下,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. 如果數(shù)據(jù)庫的表的字段和領(lǐng)域模型的字段不對應(yīng),如何處理呢?比如本文的TTable表是在TTT Schema下面的, 而其他表示設(shè)計(jì)在DBO下面,最方便的方式是使用fluent API, 具體代碼如請參見 TTableConfiguration Class和 OnModelCreating()方法,可配置的粒度非常細(xì),比如可以配置領(lǐng)域模型和數(shù)據(jù)庫的哪個Schema的哪張表的哪一列對應(yīng),本文是將TTable 類的數(shù)據(jù)庫表配置為了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. 如果需要多個操作同時成功或者失敗,需要手動開啟事務(wù),具體代碼如下,
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. 分頁查詢是網(wǎng)站設(shè)計(jì)的常用功能,一個簡單的真分頁查詢方法如下如下所示,
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();
}
}總結(jié)
本文對最新版本的Entity Framework進(jìn)行增刪改查操作給出了詳盡的解釋,并且給出了數(shù)據(jù)庫和領(lǐng)域模型代碼分開設(shè)計(jì)的完整解決方案,同時介紹了手動數(shù)據(jù)庫表和領(lǐng)域模型映射,數(shù)據(jù)庫事務(wù)實(shí)現(xiàn),分頁查詢等常用功能,希望對大家有所幫助。
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
- C#在Entity Framework中實(shí)現(xiàn)事務(wù)回滾
- Entity?Framework代碼優(yōu)先Code?First入門
- Entity?Framework映射TPH、TPT、TPC與繼承類
- Entity Framework主從表的增刪改
- Entity?Framework配置關(guān)系
- Entity Framework主從表數(shù)據(jù)加載方式
- Entity?Framework代碼優(yōu)先(Code?First)模式
- Entity?Framework使用ObjectContext類
- Entity?Framework模型優(yōu)先與實(shí)體對象查詢
相關(guān)文章
在.NET中掃描局域網(wǎng)服務(wù)的實(shí)現(xiàn)方法
下面小編就為大家分享一篇在.NET中掃描局域網(wǎng)服務(wù)的實(shí)現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
關(guān)閉子頁面刷新父頁面中部分控件數(shù)據(jù)的方法
關(guān)閉子頁面刷新父頁面中部分控件數(shù)據(jù),具體的實(shí)現(xiàn)代碼如下,感興趣的朋友可以參考下哈2013-05-05
asp.net(c#)動態(tài)修改webservice的地址和端口(動態(tài)修改配置文件)
這個問題其實(shí)并沒有我想像的那個復(fù)雜,我們都知道怎么直接修改吧,那就是修改WebConfig文件的配置節(jié)2012-12-12
.net core在服務(wù)器端獲取api傳遞的參數(shù)過程
這篇文章主要介紹了.net core在服務(wù)器端獲取api傳遞的參數(shù)過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10
.net數(shù)據(jù)庫連接池配置技巧(默認(rèn)值)
ado.net 本就有連接功能,所有.net開法基本不用去考慮連接問題,怪不得.net的連接池資料網(wǎng)上找不到。.net連接池只要在連接字符串里配制就可以了2008-12-12
.NET Core控制臺應(yīng)用程序如何使用異步(Async)Main方法詳解
這篇文章主要給大家介紹了關(guān)于.NET Core控制臺應(yīng)用程序如何使用異步(Async)Main方法的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
ASP.NET?Core?使用SignalR推送服務(wù)器日志的過程記錄
這篇文章主要介紹了ASP.NET?Core?使用SignalR推送服務(wù)器日志的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-01-01
如何使用pm2守護(hù)你的.NET Core應(yīng)用程序詳解
pm2是nodejs的一個帶有負(fù)載均衡功能的應(yīng)用進(jìn)程管理器的模塊,下面這篇文章主要給大家介紹了關(guān)于如何使用pm2守護(hù)你的.NET Core應(yīng)用程序的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-10-10

