I have a problem that I am not sure what is causing it. I have a code-first entity framework database, and on the first run everything works fine. The database gets created and no exceptions are thrown.
On the second run, when entity framework starts up and I attempt to call the 'save' function for the first time, it throws this exception: (non of my tables have a column named CreatedOn)
MySql.Data.MySqlClient.MySqlException: 'Unknown column 'CreatedOn' in 'field list''
This is my class
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public partial class MyContext : DbContext
{
public MyContext()
: base("name=MyContext")
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Migrations.Configuration>());
if (Database.Exists())
{
if (!Database.CompatibleWithModel(false))
{
Database.Initialize(true);
}
}
}
...Defining DbSets
I am using migrations as well.
I was originally getting this exception on startup as well, but I removed some code that I was adding into the database on startup. I figured the connection wasn't yet ready.. I am assuming it has something to do with the connection not being fully established but I don't know how to properly ensure we are connected before leaving my UnitOfWork class. So I have this class that connects up all of my database tables to my context:
public UnitOfWork(MyContext contextMain)
{
_context = contextMain;
_context.Database.CommandTimeout = 600;
PartFamilies = new PartFamilyRepository(_context);
PartNumbers = new PartNumberRepository(_context);
Measurements = new MeasurementRepository(_context);
Attributes = new AttributeRepository(_context);
ProgramNumbers = new ProgramNumberRepository(_context);
FamilyBiases = new FamilyBiasRepository(_context);
BagQuantities = new BagQuantityRepository(_context);
UndetectableMods = new UndetectableRepository(_context);
OverLapParts = new OverLapRepository(_context);
PartGroups = new PartGroupRepository(_context);
Settings = new SettingsRepository(_context);
CreateDefaultDatabase(contextMain.Database.CreateIfNotExists(), _context);
}
And lastly when I want to use my database, I call it like this:
using (var _unitOfWork = new UnitOfWork(new MyContext())){}
Update: I tried to turn off lazy-loading for Entity Framework and that did not affect the exception. I still receive it.
Here is a clue that might help someone with more knowledge:
This ObjectContext has not ran yet, and if I click that icon to run the threads I no longer get this exception. I don't know how to force those threads to run on creation.
I am using the following:
.NET Framework 4.8
EntityFramework 6.4.4
Mysql.Data 8.0.21
MySql.Data.EntityFramework 8.0.21