0

We are working with multiple people on a software landscape. And therefore we find it convenient to work in parallel. So the C# code for the entity is being added, while the table definition is being created (db first approach).

My question is, can this MyEntity, and DbSet be added already to the C# code in the context, without EF throwing exceptions, because the DB table is not in the database yet. This would allow the C# code development to continue (creating repository, provider, validations, etc) in the meanwhile. Of course under the condition that the DbSet is not being used in the C# code.

So is EF fine with the DbSet being part of the context, while the table for MyEntity does not exist in the database yet?

Mike de Klerk
  • 11,906
  • 8
  • 54
  • 76
  • Why not simply try it out? – MakePeaceGreatAgain Nov 05 '18 at 11:03
  • I did. See the answer below. I could not find an answer to this on the internet yet. Maybe I was not efficient in my search. Anyway, I thought it could be useful for future reference, share my findings with colleagues easily and maybe for some future searchers as well. – Mike de Klerk Nov 05 '18 at 11:04
  • Well, I didn´t say your **answer** wasn´t well reasearched, but your **question** is. I agree that your question might help future readers. – MakePeaceGreatAgain Nov 05 '18 at 11:11

1 Answers1

0

Yes you can add the entity to the context, without having the table. I verified this with a test project. See the code below. It connects to an existing DB on the local machine. The context is created without any issues. You can even use the Entities property, add an Entity, and SaveChanges(). It will create the table for you the first time. The second time the table is not there (because it got removed manually after creation for instance), it will throw an exception.

It will throw an exception because it keeps records of the state of the database in __MigrationHistory.

    using System.Data.Entity;

namespace EntityFrameWorkMissingTableTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new MyContext("Data Source=localhost;Initial Catalog=MyContext;Integrated Security=True"))
            {
                context.Entities.Add(new Entity());
                context.SaveChanges();
            }
        }

        public class MyContext : DbContext
        {
            public MyContext(string connectionString)
                : base(connectionString)
            {

            }

            public DbSet<Entity> Entities { get; set; }
        }

        public class Entity
        {
            public int Id { get; set; }
        }
    }
}
Mike de Klerk
  • 11,906
  • 8
  • 54
  • 76
  • 1
    adding an DbSet to the context alters the model hash, requiring a migration/recreation of the database (if you use one of the existing initializers). The deletion of the table was done manually, not with the migration Down() method I assume, which means the model hash does match, but not the db schema, resulting in an exception when EF tries to access the table that has been removed. – DevilSuichiro Nov 05 '18 at 11:05