Entity Framework - MySQL - Unknown Exception

Viewed 153

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: enter image description here

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

1 Answers

If you check Just My Code in debugging options you should stop seeing this. It is explained here:

Error "column c.CreatedOn does not exist..." in PostgreSQL logs during code first context initialization using the Devart dotConnect provider

If you don't want to turn on Just My Code, then you could add exception handling where it is being thrown. The following is not elegant but should work:

try
{
   ...
}
catch (MySqlException e)
{
   if(!e.Message.Equals("Unknown column 'CreatedOn' in 'field list'")
   {
      throw;
   }
}
Related