Skip to main content

Posts

Showing posts from January, 2017

#SQL: How to Drop Database in SQL Server by Closing Existing Connections

How to Drop Database in SQL Server when the users are connected to a SQL Server Database. You may need to closing existing connections in a database before restoring the database, before detaching database, get database in SINGLE_USER mode etc. If you try dropping a database when users are connected to the SQL Server Database you will receive the below mentioned error message. Error Message Drop failed for Database 'RajDB'. (Microsoft.SqlServer.Smo) Cannot drop database "" because it is currently in use. (Microsoft SQL Server, Error: 3702) USE [master] GO ALTER DATABASE RajDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE GO /* Query to Drop Database in SQL Server  */ DROP DATABASE RajDB GO

#EF : DbEntityValidationException - How can I easily tell what caused the error?

While calling  SaveChanges  on my  DbContext , I get the following exception: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. This is all fine and dandy, but I don't want to attach a debugger every time this exception occurs. More over, in production environments I cannot easily attach a debugger so I have to go to great lengths to reproduce these errors. How can I see the details hidden within the  DbEntityValidationException ? Answer :  The easiest solution is to override SaveChanges on your entities class. You can catch the DbEntityValidationException, unwrap the actual errors and create a new DbEntityValidationException with the improved message. Create a partial class next to your SomethingSomething.Context.cs file. Use the code at the bottom of this post. That's it. Your implementation will automatically use the overriden Save...