How do I insert data into my database using Entity Framework .NET Core?

c# entity-framework-core

Question

If I was using the .NET framework version of EF, I'd simply do using(var db = new dbContext()) and I'd be able to instantiate a new context and insert data using db.<tableName>.add()

However, this doesn't work for .NET core. When I attempt to instantiate a new context, I get an error message requiring that a

DbContextOptions object is passed through to the context object to instantiate it.

This is DbContext class:

public SolicitorContext(DbContextOptions<SolicitorContext> options) : base(options)
{
}

Obviously the options object needs to be passed through here to init the object.

1
2
10/15/2018 6:41:19 PM

Popular Answer

var dbOptions = new DbContextOptionsBuilder<YOURCONTEXT>()
            .UseSqlServer(connectionString: Environment.GetEnvironmentVariable("YOURCONNSTRING"))
            .Options;

using (var yourDB = new YOURCONTEXT(dbOptions))
{
}
2
10/15/2018 3:56:25 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow