日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

.netcore mysql_.netcore基于mysql的codefirst

發(fā)布時間:2023/12/9 数据库 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .netcore mysql_.netcore基于mysql的codefirst 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

.netcore基于mysql的codefirst

此文僅是對于netcore基于mysql的簡單的codefirst實現(xiàn)的簡單記錄。示例為客服系統(tǒng)消息模板的增刪改查實現(xiàn)

第一步、創(chuàng)建實體項目,并在其中建立對應(yīng)的實體類,以及數(shù)據(jù)庫訪問類

須引入Pomelo.EntityFrameworkCore.MySql和Microsoft.EntityFrameworkCore

///

///消息模板///

public classMessatgeTemplate

{///

///id///

[Key]public System.Guid Id { get; set; }///

///名稱///

[MaxLength(250)]public string Name { get; set; }///

///關(guān)鍵字///

[MaxLength(500)]public string Keywords { get; set; }///

///內(nèi)容///

public string Content { get; set; }///

///語言///

[MaxLength(80)]public string CultureCode { get; set; }///

///類別///

[MaxLength(250)]public string Category { get; set; }///

///備注信息///

public string Remark { get; set; }///

///是否啟用///

public bool Enabled { get; set; }///

///創(chuàng)建時間///

public DateTime Created { get; set; }///

///最近修改時間///

public DateTime LastModified { get; set; }

}

///

///

///

public classCustomerServiceContext : DbContext

{public CustomerServiceContext(DbContextOptionsoptions)

:base(options)

{

}///

///配置///

///

protected override voidOnConfiguring(DbContextOptionsBuilder optionsBuilder)

{base.OnConfiguring(optionsBuilder);

}public virtual DbSet MessatgeTemplate { get; set; }

}

第二步、創(chuàng)建業(yè)務(wù)服務(wù)項目,添加對應(yīng)的業(yè)務(wù)處理類

///

///客服業(yè)務(wù)統(tǒng)一接口///

public interfaceICustomerServicerSerice

{

}

///

///模板業(yè)務(wù)類///

public classMessageTemplateService : ICustomerServicerSerice

{privateCustomerServiceContext _CustomerServiceContext;publicMessageTemplateService(CustomerServiceContext customerServiceContext)

{this._CustomerServiceContext =customerServiceContext;

}///

///創(chuàng)建模板///

public voidCreateMessageTemplate()

{//todo create

}///

///修改模板///

public voidModifyMessageTemplate()

{//TODO modified

}///

///獲取模板///

///

public ListGetMessageTemplate()

{//TODO根據(jù)傳入?yún)?shù)查詢模板

return null;

}///

///刪除///

public voidDeleteMessageTemplate(Guid id)

{var template = _CustomerServiceContext.MessatgeTemplate.FirstOrDefault(p => p.Id ==id);if (template != null)

{

_CustomerServiceContext.MessatgeTemplate.Remove(template);

_CustomerServiceContext.SaveChanges();

}

}///

///批量設(shè)置是否可用///

///

///

public void BatchEnableTemplate(List ids, boolenabled)

{//TODO set enabled

}

}

第三步、創(chuàng)建webapi項目,并在其中建立對應(yīng)的實體類,以及數(shù)據(jù)庫訪問類

須引入Microsoft.EntityFrameworkCore和Microsoft.EntityFrameworkCore.Design.

1、在appsettings.json中配置數(shù)據(jù)連接

"ConnectionStrings": {"CumstomerServiceConnection": "server=localhost;port=3306;database=CustomerServiceCenter;uid=root;pwd=root23456;CharSet=utf8"}

2、修改startup.cs類的ConfigureServices注入db訪問類

services.AddDbContext(p => p.UseMySql(Configuration.GetConnectionString("CumstomerServiceConnection")));

3、設(shè)置該webapi項目為啟動項目,在nuget程序包控制臺選擇第一步中創(chuàng)建的實體類項目依次運行如下腳本創(chuàng)建數(shù)據(jù)庫

1)add-migration init

2) update-database

上面的兩個命令中,add-migration表示添加數(shù)據(jù)遷移,其實就是將實體變更轉(zhuǎn)換成sql(add-migraion后面的init只是個名稱,可以自行定義),update-database是將生成的數(shù)據(jù)遷移信息更新到數(shù)據(jù)庫

另外一些可能用到的命令

remove-migration? ---將最近生成的遷移(沒有更新到數(shù)據(jù)庫的)移除掉

script-migration --生成數(shù)據(jù)庫遷移相應(yīng)的腳本

------------------------

命令執(zhí)行成功后,就可以在數(shù)據(jù)庫中找到創(chuàng)建好的數(shù)據(jù)庫和表了

script-datatable可以生成數(shù)據(jù)庫腳本語句

好了,以上就完成了codefirst的簡單操作了。不過跟著第二步的業(yè)務(wù)類其實沒啥關(guān)系!!!如果只是進行數(shù)據(jù)遷移確實沒有service層什么事,但是業(yè)務(wù)邏輯要走通,實現(xiàn)crud,業(yè)務(wù)層還是必須的。

---------------------------------------------------------------------------------------------------------------------

以下就順帶寫下controller中引入service,實現(xiàn)簡單的刪除業(yè)務(wù)邏輯(其它的業(yè)務(wù)邏輯---略)

在webapi創(chuàng)建MessageTemplateController

[Route("api/[controller]")]

[ApiController]public classMessageTemplateController : ControllerBase

{privateMessageTemplateService _MessageTemplateService;publicMessageTemplateController(MessageTemplateService messageTemplateService)

{this._MessageTemplateService =messageTemplateService;

}///

///刪除///

///

[HttpPost("DeleteMessageTemplate")]public voidDeleteMessageTemplate(Guid id)

{

_MessageTemplateService.DeleteMessageTemplate(id);

}//TODO其他操作:查詢,新增,修改等

}

修改Startup.cs類,注入服務(wù)

public voidConfigureServices(IServiceCollection services)

{

services.AddControllers();

services.AddDbContext(p => p.UseMySql(Configuration.GetConnectionString("CumstomerServiceConnection")));#region 注入業(yè)務(wù)服務(wù)Assembly assembly= Assembly.Load("Qingy.DotNetCoreStudy.CustomerServiceService");

List types = assembly.GetTypes().Where(u => u.IsClass && !u.IsAbstract && !u.IsGenericType&& u.GetInterfaces().Any(p => p == typeof(ICustomerServicerSerice))

).ToList();foreach (var item intypes)

{

services.AddTransient(item);

}#endregion}

綜合以上,就可以將api到db的完整實現(xiàn)了。

總結(jié)

以上是生活随笔為你收集整理的.netcore mysql_.netcore基于mysql的codefirst的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。