Created a model based on the database. On the server there are several identical to it. The user can choose which database to connect to. But at the same time, the problem arises that the user selects another database in which the required tables are missing and an error will occur. How can I determine if the required tables are present in the selected database? Is there an Entity Framework for this tool?

Used Database First

  • 3
    A solution that does not require deep knowledge of EF (therefore not the answer). Execute any simple request by wrapping it in a try / catch. If an exception is caught that should occur when the model does not conform to the database scheme, the user cannot be told there. - Uranus
  • And can you clarify about the approach used? If CodeFirst is needed, the @QodirbekMakharov answer is perfect because of its simplicity. If DBFirst, then there is more difficult, but I can offer options. - Vadim Ovchinnikov

2 answers 2

If CodeFirst:

bool isCompatible = db.Database.CompatibleWithModel(true); 

true - the model and the database are identical, false - the model and the database are not identical.

    I did not find such built-in tools in EF. But my solution is

    1. We enable the possibility of migrations for the context database. To do this, enter the command

       enable-migrations 

    of

    Tools → Library Package Manager → Package Manager Console

    After executing the command, the folder Migrations should be created in the selected project, and inside this folder is the Configuration.cs file, which contains the migration settings.
    In the constructor of this automatically generated class, we disable automatic migration by setting the AutomaticMigrationsEnabled property to false . For example,

     internal sealed class Configuration : DbMigrationsConfiguration<ContosoUniversity.DAL.SchoolContext> { public Configuration() { AutomaticMigrationsEnabled = false; } protected override void Seed(ContosoUniversity.DAL.SchoolContext context) { // This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. Eg // // context.People.AddOrUpdate( // p => p.FullName, // new Person { FullName = "Andrew Peters" }, // new Person { FullName = "Brice Lambson" }, // new Person { FullName = "Rowan Miller" } // ); // } } 

    Now, when initializing the database context, if the database schema does not fit the described model, an exception will be thrown (without trying to tune the database schema for the model).

    Perform explicit context initialization by wrapping it in try / catch

     try { using(var context = new MyDb()) { context.Database.Initialize(false); } // БД подходит под модель } catch { // бд не соответствует схеме } 

    This code can be wrapped in some method that returns a boolean value with the result of checking the conformity of the model and database schema. If an exception occurred, then the DB schema does not match the model.

    1. You can also execute a sql script to check for the existence of a context in the database using the database:

       using(var dbContext = new MyDb()) { int result = (dbContext as IObjectContextAdapter).ObjectContext.ExecuteStoreQuery<int>(@" IF EXISTS (SELECT * FROM sys.tables WHERE name = 'TableName') SELECT 1 ELSE SELECT 0 ").SingleOrDefault(); } 
    • AK asked you to give you 50 reputation points. :) - Nick Volynkin
    • thanks to the kind person :) - Alexcei Shmakov