Is there a command to check to see if a database exists from Entity Framework?

后端 未结 2 1537
天涯浪人
天涯浪人 2020-12-20 15:23

I may have worded the question poorly but in my global.asx file i use

 if (System.Diagnostics.Debugger.IsAttached)
        {
            var test = new Test         


        
2条回答
  •  春和景丽
    2020-12-20 15:49

    In Entity Framework Core it works like this:

    namespace Database
    {
        using Microsoft.EntityFrameworkCore.Infrastructure;
        using Microsoft.EntityFrameworkCore.Storage;
    
        public partial class MyContextClass
        {
            /// 
            /// Checks if database exists
            /// 
            /// 
            public bool Exists()
            {
                return (this.Database.GetService() as RelationalDatabaseCreator).Exists();
            }
        }
    }
    

    Make sure the class name equals your database Context class name and is in the same namespace.

    Use it like this:

    var dbExists = (MyContextClass)db.Exists()
    

    Source: StackOverflow Answer

提交回复
热议问题