Zugriff auf DBContext in einer C# -Konsole -AnwendungC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Zugriff auf DBContext in einer C# -Konsole -Anwendung

Post by Anonymous »

Ich habe versucht, dies herauszufinden, aber ich stecke fest. 'Bolt on' eine Konsolenanwendung und greifen Sie auf alle Leckereien zu, die ich bereits aufgebaut habe. Ich scheine in einem Chaos statischer Objekte und DI- und NULL -Parameter zu sein. Wie auch immer, hier ist eine vereinfachte Version meines Codes. < /P>

Code: Select all

namespace SimpleExample
{
class Program
{
private static ApplicationDbContext _appDbContext;

public Program(ApplicationDbContext appDbContext)
{
_appDbContext = appDbContext;
}

static void Main(string[] args)
{
var instance = new Program();  // this doesn't work!
var instance = new Program(_appDbContext);  // neither does this!
instance.GetData();
}

private void GetData()
{
Console.WriteLine("Let's read some data! Press a key to continue.");
Console.ReadLine();

var data = "my data";
var result = GetId(data);
}

private string GetId(string original)
{
var data = _appDbContext.Data
.Where(x => x.Name == original.Trim)
.FirstOrDefault();

return data;
}
}

}
< /code>
Ich erhalte den klassischen < /p>

'Eine Objektreferenz ist für das nicht statische Feld erforderlich' < /p >
< /blockquote>
Fehler. Dann habe ich von der Untersuchung hier die Dinge in statisch geändert und dann wird alles null. Ich versuche auch zu injizieren < /p>
private ManagerService _managerService;
, aber die gleichen Fehler erhalten. < /p>
private static ApplicationDbContext _appDbContext = new ApplicationDbContext();
< /code>
Wie unten vorgeschlagen, erhalte ich einen Fehler. Erforderliche formale
Parameter 'Optionen' von
'ApplicationDbContext.ApplicationDBContext(DbContextOptions)'
< /blockquote>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post