12

I have to add data to my database using a Console Application. In the Main() method I added:

var services = new ServiceCollection();
var serviceProvider = services.BuildServiceProvider();
var connection = @"Server = (localdb)\mssqllocaldb; Database = CryptoCurrency; Trusted_Connection = True; ConnectRetryCount = 0";
services.AddDbContext<CurrencyDbContext>(options => options.UseSqlServer(connection));

In another class I add functionality to work with database, and made it like a Web Api application and added my DbContext into constructor:

public AutoGetCurrency(CurrencyDbContext db) =>  this.db = new CurrencyDbContext();

This gives the following error:

Object reference not set to an instance of an object

I tried to add a default constructor without parameters, and it still gives the same error.

Please tell me how I can use DI in .Net core console application ?

Uladz Kha
  • 2,154
  • 4
  • 40
  • 61

2 Answers2

16

Add services to collection before building provider. In your example you are adding services after already having built the provider. Any modifications made to the collection have no effect on the provider once built.

var services = new ServiceCollection();
var connection = @"Server = (localdb)\mssqllocaldb; Database = CryptoCurrency; Trusted_Connection = True; ConnectRetryCount = 0";
services.AddDbContext<CurrencyDbContext>(options => options.UseSqlServer(connection));
//...add any other services needed
services.AddTransient<AutoGetCurrency>();

//...

////then build provider 
var serviceProvider = services.BuildServiceProvider();

Also in the constructor example provided you are still initializing the db.

public AutoGetCurrency(CurrencyDbContext db) =>  this.db = new CurrencyDbContext();

The injected db is not being used. You need to pass the injected value to the local field.

public AutoGetCurrency(CurrencyDbContext db) =>  this.db = db;

Once configured correctly you can then resolve your classes via the provider and have the provider create and inject any necessary dependencies when resolving the requested service.

var currency = serviceProvider.GetService<AutoGetCurrency>();
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Thank you for you answer,please tell me , i have constructor with parameter, i need to create instance of this class in Main(), var currency = new AutoGetCurrency(/*What i have to write here*/); ??? – Uladz Kha Jan 21 '18 at 20:53
  • 1
    @UladzimirKhadakouski you can also register the class with the service collection and have the provider create and inject any necessary dependencies. – Nkosi Jan 21 '18 at 20:55
  • I still don't get it. What if I just need the dbcontext in main? – tnk479 Jun 09 '19 at 20:01
  • @tnk479 there is nothing stopping you from initializing everything yourself. But that is a lot to do when there are services/containers that can do most of the heavy lifting. – Nkosi Jun 09 '19 at 20:11
  • but when the database change, for example, new record inserted, the DB doesn't get the latest changes the I have to repeat `serviceProvider = services.BuildServiceProvider();` every time. :((((( – Peyman Majidi Jan 04 '21 at 08:02
0

https://github.com/Akeraiotitasoft/ConsoleDriving Nuget: Akeraiotitasoft.ConsoleDriving This API is beta. If someone thinks that it does work properly please send an email. A release will be made.

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/29899061) – m4n0 Sep 23 '21 at 10:29