我们可以使用ASP.NET Boilerplate和Dapper而不是Entity Framework Core吗?

问题描述 投票:-1回答:1

我是ASP.NET Boilerplate和Dapper的新手。从本教程中我了解到ASP.NET Boilerplate与Entity Framework Core一起使用。我们可以使用其他数据访问层而不是EF Core吗? ASP.NET Boilerplate与Dapper兼容吗?

entity-framework-core dapper aspnetboilerplate
1个回答
0
投票

当然!你可以使用Dapper。

安装

在开始之前,您需要将https://www.nuget.org/packages/Abp.Dapper,EF Core,EF 6.x或NHibernate ORM NuGet包安装到您要使用的项目中。


模块注册

首先,您需要在注册模块的模块上添加AbpDapperModule的DependsOn属性

[DependsOn(
     typeof(AbpEntityFrameworkCoreModule),
     typeof(AbpDapperModule)
)]
public class MyModule : AbpModule
{
    public override void Initialize()
    {
        IocManager.RegisterAssemblyByConvention(typeof(SampleApplicationModule).GetAssembly());
    }
}

表映射的实体

您可以配置映射。例如,Person类映射到以下示例中的Persons表:

public class PersonMapper : ClassMapper<Person>
{
    public PersonMapper()
    {
        Table("Persons");
        Map(x => x.Roles).Ignore();
        AutoMap();
    }
}

样本模块:

[DependsOn(
     typeof(AbpEntityFrameworkModule),
     typeof(AbpDapperModule)
)]
public class MyModule : AbpModule
{
    public override void Initialize()
    {
        IocManager.RegisterAssemblyByConvention(typeof(SampleApplicationModule).GetAssembly());
        DapperExtensions.SetMappingAssemblies(new List<Assembly> { typeof(MyModule).GetAssembly() });
    }
}

用法

注册AbpDapperModule后,您可以使用Generic IDapperRepository接口(而不是标准IRepository)来注入dapper存储库。

public class SomeApplicationService : ITransientDependency
{
    private readonly IDapperRepository<Person> _personDapperRepository;
    private readonly IRepository<Person> _personRepository;

    public SomeApplicationService(
        IRepository<Person> personRepository,
        IDapperRepository<Person> personDapperRepository)
    {
        _personRepository = personRepository;
        _personDapperRepository = personDapperRepository;
    }

    public void DoSomeStuff()
    {
        var people = _personDapperRepository.Query("select * from Persons");
    }
}

看看这些链接:

© www.soinside.com 2019 - 2024. All rights reserved.