0

I want pass my entities to my SQL Server database from API in .Net Core. I need an extension or module that can be added to Container builder and Helps me to use in ConfigureServices method in Startup.cs I found a project according to my needs (https://github.com/devmentors/DNC-DShop), but this sample has been implemented with MongoDB. I want to implement the same thing with SQLSERVER DB and EF ORM.

This code is inside startup file For Mongo Sample :

builder.AddMongo();
builder.AddMongoRepository<Cart>("Carts");
builder.AddMongoRepository<Customer>("Customers");
builder.AddMongoRepository<Product>("Products");

This code in applicationsetting.json file:

  "mongo": {
    "connectionString": "mongodb://localhost:27017",
"database": "customers-service",
    "seed": false
},

This Code is inside Extension.cs:

public static class Extensions
    {

        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();
    }
  • 1
    Possible duplicate of [Persistance datalayer in EF core ,dynamic EF. Separate EF from models](https://stackoverflow.com/questions/56273847/persistance-datalayer-in-ef-core-dynamic-ef-separate-ef-from-models) – MrMaavin May 28 '19 at 07:56
  • Use the SQL server :) https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.sqlserverdbcontextoptionsextensions.usesqlserver?view=efcore-2.1 – Philippe May 28 '19 at 08:28
  • @Philippe as i saw i think your link just describes the DB option not the model option.am i right? – Ehsan Akbar May 28 '19 at 08:48
  • Looks like exact copy this question https://stackoverflow.com/questions/56273847/persistance-datalayer-in-ef-core-dynamic-ef-separate-ef-from-models – trailmax May 28 '19 at 09:37

1 Answers1

0

I'll describe a solution with a project I'm currently working on (Sppd.TeamTuner), which supports multiple database providers.

There is a *.DataAccess.EF.csproj project, registering the required services and configuring the DbContext. It is important to understand that this project does NOT register the used database instance, it merely specifies how the entities will be stored. The interesting classes for you are both partial TeamTunerContext.cs classes here. I've added this paragraph as in your example it seems your coupled with MongoDb, which shouldn't be necessary.

What you are looking for, telling what DB to use, is being done in one of the dedicated DB provider projects (MsSql, Sqlite, InMemory). As you want to use SQL, this line is relevant:

services.AddDbContext<TeamTunerContextMsSql>(options => options.UseSqlServer(databaseConfig.ConnectionString))

For this the nuget package Microsoft.EntityFrameworkCore.SqlServer, containing the extension method UseSqlServer is being referenced. (If you references asp.net core in your project I think this is already included).

Philippe
  • 1,949
  • 4
  • 31
  • 57