亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

.net如何優(yōu)雅的使用EFCore實(shí)例詳解

 更新時(shí)間:2022年11月28日 16:04:37   作者:BruceNeter  
這篇文章主要為大家介紹了.net如何優(yōu)雅的使用EFCore實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

正文

EFCore是微軟官方的一款ORM框架,主要是用于實(shí)體和數(shù)據(jù)庫(kù)對(duì)象之間的操作。功能非常強(qiáng)大,在老版本的時(shí)候叫做EF,后來(lái).net core問(wèn)世,EFCore也隨之問(wèn)世。

本文我們將用一個(gè)控制臺(tái)項(xiàng)目Host一個(gè)web服務(wù),并且使用本地Mysql作為數(shù)據(jù)庫(kù),使用EFCore的Code First模式進(jìn)行數(shù)據(jù)操作。

DBSet清除計(jì)劃

以前使用EF/EFCore的開(kāi)發(fā)者應(yīng)該都記得,需要在DBContext里寫好多DBSet,一個(gè)表對(duì)應(yīng)一個(gè)DBSet,然后在其他地方操作這些DBSet對(duì)相關(guān)的表進(jìn)行增刪改查。作為一個(gè)開(kāi)發(fā),這些重復(fù)操作都是我們希望避免的,我們可以利用反射機(jī)制將這些類型通過(guò)框架自帶的方法循環(huán)注冊(cè)進(jìn)去。

1.EF實(shí)體繼承統(tǒng)一的接口,方便我們反射獲取所有EF實(shí)體,接口可以設(shè)置一個(gè)泛型,來(lái)泛化我們的主鍵類型,因?yàn)榭赡艽嬖诓煌谋淼闹麈I類型也不一樣。

統(tǒng)一的EF實(shí)體接口

public interface IEFEntity<TKey>
{
    public TKey Id { get; set; }
}

統(tǒng)一的接口實(shí)現(xiàn)類

public abstract class AggregateRoot<TKey> : IEFEntity<TKey>
{
    public TKey Id { get; set; }
}

用戶實(shí)體類

public class User : AggregateRoot<string>
{
    public string UserName { get; set; }
    public DateTime Birthday { get; set; }
    public virtual ICollection<Book> Books { get; set; }
}

2.利用反射獲取某個(gè)程序集下所有的實(shí)體類

public class EFEntityInfo
{
    public (Assembly Assembly, IEnumerable<Type> Types) EFEntitiesInfo => (GetType().Assembly, GetEntityTypes(GetType().Assembly));
    private IEnumerable<Type> GetEntityTypes(Assembly assembly)
    {
        //獲取當(dāng)前程序集下所有的實(shí)現(xiàn)了IEFEntity的實(shí)體類
        var efEntities = assembly.GetTypes().Where(m => m.FullName != null
                                                        && Array.Exists(m.GetInterfaces(), t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEFEntity<>))
                                                        && !m.IsAbstract && !m.IsInterface).ToArray();
        return efEntities;
    }
}

3.DBContext實(shí)現(xiàn)類中OnModelCreating方法中注冊(cè)這些類型

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    //循環(huán)實(shí)體類型,并且通過(guò)Entity方法注冊(cè)類型
    foreach (var entityType in Types)
    {
        modelBuilder.Entity(entityType);
    }
    base.OnModelCreating(modelBuilder);
}

至此為止所有的實(shí)體類都被注冊(cè)到DBContext中作為DBSets,再也不需要一個(gè)個(gè)寫DBSet了,可以用過(guò)DbContext.Set<User>()獲取用戶的DBSet。

IEntityTypeConfiguration(表配置)

用數(shù)據(jù)庫(kù)創(chuàng)建過(guò)表的同學(xué)都知道,在設(shè)計(jì)表的時(shí)候,可以給表添加很多配置和約束,在Code First模式中,很多同學(xué)都是在對(duì)象中通過(guò)注解的方式配置字段。如下就配置了用戶名是不能為NULL和最大長(zhǎng)度為500

[Required]
[MaxLength(500)]
public string UserName { get; set; }

也有的同學(xué)在DbContext中的OnModelCreating方法配置

modelBuilder.Entity<User>().Property(x => x.UserName).IsRequired();

這兩種方法,前者入侵行太強(qiáng),直接代碼耦合到實(shí)體類中了,后者不夠清楚,把一大堆表的配置寫在一個(gè)方法里,當(dāng)然了很多人說(shuō)可以拆分不同的方法或者使用注釋分開(kāi)。但是!不夠優(yōu)雅!
我們可以使用IEntityTypeConfiguration接口實(shí)現(xiàn)我們所想的優(yōu)雅的表配置。
1.創(chuàng)建一個(gè)配置基類,繼承自IEntityTypeConfiguration,做一些通用的配置,比如設(shè)置主鍵,一般都是id啦,還有軟刪除等。

public abstract class EntityTypeConfiguration<TEntity, TKey> : IEntityTypeConfiguration<TEntity>
       where TEntity : AggregateRoot<TKey>
{
    public virtual void Configure(EntityTypeBuilder<TEntity> builder)
    {
        var entityType = typeof(TEntity);
        builder.HasKey(x => x.Id);
        if (typeof(ISoftDelete).IsAssignableFrom(entityType))
        {
            builder.HasQueryFilter(d => EF.Property<bool>(d, "IsDeleted") == false);
        }
    }
}

2.創(chuàng)建用戶實(shí)體/表獨(dú)有的配置,比如設(shè)置用戶名的最大長(zhǎng)度,以及seed一些數(shù)據(jù)

public class UserConfig : EntityTypeConfiguration<User, string>
{
    public override void Configure(EntityTypeBuilder<User> builder)
    {
        base.Configure(builder);
        builder.Property(x => x.UserName).HasMaxLength(50);
        //mock一條數(shù)據(jù)
        builder.HasData(new User()
        {
            Id = "090213204",
            UserName = "Bruce",
            Birthday = DateTime.Parse("1996-08-24")
        });
    }
}

當(dāng)然還有很多配置可以設(shè)置,比如索引,導(dǎo)航屬性,唯一鍵等。如下圖書實(shí)體

public class BookConfig : EntityTypeConfiguration<Book, long>
{
    public override void Configure(EntityTypeBuilder<Book> builder)
    {
        base.Configure(builder);
        builder.Property(x => x.Id).ValueGeneratedOnAdd(); //設(shè)置book的id自增
        builder.Property(x => x.BookName).HasMaxLength(500).IsRequired();
        builder.HasIndex(x => x.Author);//作者添加索引
        builder.HasIndex(x => x.SN).IsUnique();//序列號(hào)添加唯一索引
        builder.HasOne(r => r.User).WithMany(x=>x.Books)
            .HasForeignKey(r => r.UserId).IsRequired();//導(dǎo)航屬性,本質(zhì)就是創(chuàng)建外鍵,雖然查詢很方便,生產(chǎn)中不建議使用?。。?
    }
}

3.DBContext中應(yīng)用配置

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.HasCharSet("utf8mb4 ");
    var (Assembly, Types) = _efEntitysInfo.EFEntitiesInfo;
    foreach (var entityType in Types)
    {
        modelBuilder.Entity(entityType);
    }
    //只需要將配置類所在的程序集給到,它會(huì)自動(dòng)加載
    modelBuilder.ApplyConfigurationsFromAssembly(Assembly);
    base.OnModelCreating(modelBuilder);
}

Repository(倉(cāng)儲(chǔ))

這個(gè)不過(guò)分介紹,特別是基于http的微服務(wù)中基本都有這個(gè)。

1.創(chuàng)建一個(gè)倉(cāng)儲(chǔ)基類,對(duì)于不同的實(shí)體,創(chuàng)建一樣的增刪改查方法。

簡(jiǎn)單寫幾個(gè)查詢的方法定義。

public interface IAsyncRepository<TEntity, Tkey> where TEntity : class
{
    IQueryable<TEntity> All();
    IQueryable<TEntity> All(string[] propertiesToInclude);
    IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> filter);
    IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> filter, string[] propertiesToInclude);
}

2.創(chuàng)建倉(cāng)儲(chǔ)實(shí)現(xiàn)類,將DBContext注入到構(gòu)造中

public class GenericRepository<TEntity, Tkey> : IAsyncRepository<TEntity, Tkey> where TEntity : class
{
    protected readonly LibraryDbContext _dbContext;
    public GenericRepository(LibraryDbContext dbContext)
    {
        _dbContext = dbContext;
    }
    ~GenericRepository()
    {
        _dbContext?.Dispose();
    }
    public virtual IQueryable<TEntity> All()
    {
        return All(null);
    }
    public virtual IQueryable<TEntity> All(string[] propertiesToInclude)
    {
        var query = _dbContext.Set<TEntity>().AsNoTracking();
        if (propertiesToInclude != null)
        {
            foreach (var property in propertiesToInclude.Where(p => !string.IsNullOrWhiteSpace(p)))
            {
                query = query.Include(property);
            }
        }
        return query;
    }
}

Autofac

1.注入DBContext到Repository的構(gòu)造方法中,并且注入Repository

public class EFCoreEleganceUseEFCoreModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        base.Load(builder);
        builder.RegisterModule<EFCoreEleganceUseDomainModule>(); //注入domain模塊
        builder.RegisterGeneric(typeof(GenericRepository<,>))//將dbcontext注入到倉(cāng)儲(chǔ)的構(gòu)造中
                .UsingConstructor(typeof(LibraryDbContext))
                .AsImplementedInterfaces()
                .InstancePerDependency();
        builder.RegisterType<WorkUnit>().As<IWorkUnit>().InstancePerDependency();
    }
}

2.Domain注入EFEntityInfo

public class EFCoreEleganceUseDomainModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<EFEntityInfo>().SingleInstance();
    }
}

數(shù)據(jù)庫(kù)配置

1.注入DBContext,從配置文件讀取數(shù)據(jù)庫(kù)配置,然后根據(jù)開(kāi)發(fā)/生產(chǎn)環(huán)境做一些特殊處理

var mysqlConfig = hostContext.Configuration.GetSection("Mysql").Get<MysqlOptions>();
var serverVersion = new MariaDbServerVersion(new Version(mysqlConfig.Version));
services.AddDbContextFactory<LibraryDbContext>(options =>
{
    options.UseMySql(mysqlConfig.ConnectionString, serverVersion, optionsBuilder =>
    {
        optionsBuilder.MinBatchSize(4);
        optionsBuilder.CommandTimeout(10);
        optionsBuilder.MigrationsAssembly(mysqlConfig.MigrationAssembly);//遷移文件所在的程序集
        optionsBuilder.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery);
    }).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
    //開(kāi)發(fā)環(huán)境可以打開(kāi)日志記錄和顯示詳細(xì)的錯(cuò)誤
    if (hostContext.HostingEnvironment.IsDevelopment())
    {
        options.EnableSensitiveDataLogging();
        options.EnableDetailedErrors();
    }
});

項(xiàng)目架構(gòu)和源碼

項(xiàng)目只是一個(gè)demo架構(gòu),并不適用于生產(chǎn),主程序是一個(gè)控制臺(tái)項(xiàng)目,只需要引用相關(guān)的包和模塊,就可以啟動(dòng)一個(gè)web host.

全部代碼已經(jīng)全部上傳到github:https://github.com/BruceQiu1996/EFCoreDemo該項(xiàng)目是一個(gè)可以啟動(dòng)運(yùn)行的基于.net6的控制臺(tái)項(xiàng)目,啟動(dòng)后會(huì)啟動(dòng)一個(gè)web host和一個(gè)swagger頁(yè)面。

以上就是.net如何優(yōu)雅的使用EFCore實(shí)例詳解的詳細(xì)內(nèi)容,更多關(guān)于.net使用EFCore的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論