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 ?

2 Answers 2

17

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>();
Sign up to request clarification or add additional context in comments.

5 Comments

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*/); ???
@UladzimirKhadakouski you can also register the class with the service collection and have the provider create and inject any necessary dependencies.
I still don't get it. What if I just need the dbcontext in main?
@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.
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. :(((((
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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.