Manju Manju - 16 days ago 5
C# Question

Entity framework table creating c#

I have a Database named SalesERPDB and I'm trying to create a new table "TblEmployee", but getting an exception on creating the same.

public class SalesERPDB : DbContext
{
public SalesERPDB() : base("SalesERPDB")
{

}

public DbSet<Employee> Employees { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>().ToTable("TblEmployee");
base.OnModelCreating(modelBuilder);
}
}


EmployeeBusinessLayer class (Where i have a method GetEmployees() to get the employee list):

public class EmployeeBusinessLayer
{
public List<Employee> GetEmployees()
{
SalesERPDB salesDal = new SalesERPDB();
return salesDal.Employees.ToList(); // Getting an exception in this line.
}
}


Employee Class:

public class Employee
{
[Key]
public int EmployeeId { get; set; }
public string FName { get; set; }
public string LName { get; set; }
public int Salary { get; set; }
}


I'm able to hit the same connection string from a different application.

Connection string:

<connectionStrings>
<add name="SalesERPDB" connectionString="Data Source=CSCINDAI406933\\SQLEXPRESS;Initial Catalog=SalesERPDB;Integrated Security=True;" providerName="System.Data.SQLClient"></add>




Exception details:

Description: An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Exception Details: System.InvalidOperationException: Instance failure.
Stack trace: [InvalidOperationException: Instance failure.]
System.Web.Mvc.Async.<>c__DisplayClass46.b__3f() +323
System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource
1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +78
System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource
1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) +191
System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
1 retry, DbConnectionOptions userOptions) +119
System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +44
System.Web.Mvc.Async.WrappedAsyncResult
1.CallEndDelegate(IAsyncResult asyncResult) +47
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +72
System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
1 retry, DbConnectionOptions userOptions) +154
System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
1 retry, DbConnectionOptions userOptions) +21
System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource
1 retry) +141
System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource
1 retry) +362
System.Data.SqlClient.SqlConnection.Open() +96
System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<Open>b__36(DbConnection t, DbConnectionInterceptionContext c) +12
System.Data.Entity.Infrastructure.Interception.InternalDispatcher
1.Dispatch(TTarget target, Action
2 operation, TInterceptionContext interceptionContext, Action
3 executing, Action
3 executed) +72
System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext) +405
System.Data.Entity.SqlServer.<>c__DisplayClass33.<UsingConnection>b__32() +459
System.Data.Entity.SqlServer.<>c__DisplayClass1.<Execute>b__0() +15
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func
1 operation) +190
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation) +82
System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action
1 act) +194
System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action
1 act) +427
System.Data.Entity.SqlServer.SqlProviderServices.CheckDatabaseExists(SqlConnection sqlConnection, Nullable
1 commandTimeout, String databaseName) +86
System.Data.Entity.SqlServer.SqlProviderServices.DbDatabaseExists(DbConnection connection, Nullable
1 commandTimeout, Lazy
1 storeItemCollection) +347
System.Data.Entity.Core.Common.DbProviderServices.DatabaseExists(DbConnection connection, Nullable
1 commandTimeout, Lazy
1 storeItemCollection) +122
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185
System.Web.Mvc.Async.WrappedAsyncResult
1.CallEndDelegate(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult) +62
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9644037
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155


Fix for this issue would be really appreciated. Thanks in advance.

Answer

As you got the error "instance failure" based on stack trace , that might be the error with your SQL Server instance. change data source like Data Source=.\SQLEXPRESS

Comments