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

2
  • Are you using DotNet Framework or DotNet-CORE ? If "Core", then which version? 2.1 or 3.1+ ? Commented Feb 1, 2021 at 14:08
  • I am using .NET Framework 4.8, I updated the question with all of my dependencies as well. Commented Feb 1, 2021 at 14:09

1 Answer 1

3
+50

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;
   }
}
Sign up to request clarification or add additional context in comments.

5 Comments

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.
It doesn't indicate anything wrong with your code, so there is nothing to fix.
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?
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.