EF 核心中的持久性数据层,动态 EF.将 EF 与模型分开 [英] Persistance datalayer in EF core ,dynamic EF. Separate EF from models
问题描述
我想将 EF 层从我的模型中分离出来.
I want to separate EF layer from my model .
我需要一个 EF Builder 来像这样将我的模型发送给它(我找到了 mongodb 的这段代码,但我需要 EF 核心):
I need a EF Builder to send my model to it like this(I found this code for mongodb but i need for EF core) :
builder.AddMongo();
builder.AddMongoRepository<Cart>("Carts");
builder.AddMongoRepository<Customer>("Customers");
builder.AddMongoRepository<Product>("Products");
以上代码在启动文件中.
The above code is inside startup file .
我从 applicationsetting.json
文件中传递参数,如您所见:
I pass the parameters from applicationsetting.json
file as you can see :
"mongo": {
"connectionString": "mongodb://localhost:27017",
"database": "customers-service",
"seed": false
},
这是mongo示例代码:
Here is the mongo sample code :
public static class Extensions
{
public static void AddMongo(this ContainerBuilder builder)
{
builder.Register(context =>
{
var configuration = context.Resolve<IConfiguration>();
var options = configuration.GetOptions<MongoDbOptions>("mongo");
return options;
}).SingleInstance();
builder.Register(context =>
{
var options = context.Resolve<MongoDbOptions>();
return new MongoClient(options.ConnectionString);
}).SingleInstance();
builder.Register(context =>
{
var options = context.Resolve<MongoDbOptions>();
var client = context.Resolve<MongoClient>();
return client.GetDatabase(options.Database);
}).InstancePerLifetimeScope();
builder.RegisterType<EFDbInitializer>()
.As<IEFDbInitializer>()
.InstancePerLifetimeScope();
builder.RegisterType<MongoDbSeeder>()
.As<IEFDbSeeder>()
.InstancePerLifetimeScope();
}
public static void AddMongoRepository<TEntity>(this ContainerBuilder builder, string collectionName)
where TEntity : IIdentifiable
=> builder.Register(ctx => new EFRepository<TEntity>(ctx.Resolve<IMongoDatabase>(), collectionName))
.As<IMongoRepository<TEntity>>()
.InstancePerLifetimeScope();
}
我的问题是:有没有像 mongo 这样的 EF 的解决方案?
My question is :is there any solution for EF like mongo ?
mongoDb 的每一部分代码都可用.
Every part of code for mongoDb is available.
推荐答案
首先,我相信有一点困惑:
First of all, I believe there is a little confusion :
- MongoDB 是一个数据库
- Entity Core 是一个 ORM.它允许您访问存储中的数据
- MongoDB is a database
- Entity Core is an ORM. It allows you to access to data in a storage
也就是说,您可以将 EF Core 与多个数据存储一起使用,例如 MongoDB 或 SQL Server.
That said, you can use EF Core with several data storages, such as MongoDB or SQL Server for instance.
希望实体(数据访问层)和业务模型之间有一个分离层是一种很好的做法,应该鼓励.
由于主题广泛,并且存在大量文档和教程以及主题,因此我更愿意给您一些链接,而不是给您一个完整的架构.
To want a separated layer between your entities (Data Access Layer) and your business model is a good practice, and should be encouraged.
Because the topic is wide, and because lot of documentations and tutorials exists and the subject, I prefer to give you some links rather than to give you a complete architecture.
您可以查看此文档 来自 Microsoft,以查看相关层以及它们应包含的内容.
You can check this documentation from Microsoft to see the related layers, and what they should contains.
另外,我建议你参考 这个 github repo,它提供了大量基于.net 核心.
Also, I advise you to consult this github repo, which provide tons of clean architectures based on .net core.
随意挖掘这些链接,它们提供了很多宝贵的信息.
Feel free to dig a bit into those links, they are providing a lot of precious informations.
希望有帮助.
这篇关于EF 核心中的持久性数据层,动态 EF.将 EF 与模型分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!