3

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

Jay Mason
  • 446
  • 3
  • 17

1 Answers1

2

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;
   }
}
Peter Dongan
  • 1,762
  • 12
  • 17
  • Thanks, but this feels like a workaround and is not really viable for my solution. I utilize a lot of managed and unmanaged DLL's and I need the ability to see all exceptions. This just feels like I am covering up the MySQL exception being thrown. I would like to figure out how to solve it, if possible. – Jay Mason Feb 01 '21 at 14:19
  • It doesn't indicate anything wrong with your code, so there is nothing to fix. – Peter Dongan Feb 01 '21 at 14:23
  • Could it be how I created my migrations, cause my other project doesn't do this. Would it be worth resetting the migrations and remaking it? Would entity framework behave differently? – Jay Mason Feb 01 '21 at 14:24
  • I don't know whether that would affect it. I edited my answer to give a suggestion of how to ignore it without turning on Just My Code. – Peter Dongan Feb 01 '21 at 14:48
  • It still hits the exception and breaks even in a Try Catch. I just hit the checkbox to ignore this type of exception from both DLLs. Thanks for your help, this is not the way I would have liked to solve this. I would prefer that Entity Framework have a way to fix this from being thrown in the first place. – Jay Mason Feb 01 '21 at 15:22