Testing an Entity Framework database connection

前端 未结 6 1659
我在风中等你
我在风中等你 2020-12-08 04:34

I have an app that connects to a MYSQL database through the entity framework. It works 100% perfectly, but I would like to add a small piece of code that will test the conne

6条回答
  •  不思量自难忘°
    2020-12-08 05:08

    Are you just wanting to see if the DB connection is valid? If so take a look at the

    using (DatabaseContext dbContext = new DatabaseContext())
    {
         dbContext.Database.Exists();
    }
    

    http://msdn.microsoft.com/en-us/library/gg696617(v=vs.103).aspx

    and for checking if a server machine is up, DB server or web services server , try this:

    public PingReply Send( string hostNameOrAddress )

    http://msdn.microsoft.com/en-us/library/7hzczzed.aspx

提交回复
热议问题