我有一個名爲SalesERPDB的數據庫,我試圖創建一個新表「TblEmployee」,但創建相同的例外。實體框架表創建c#
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類(在那裏我有一個方法,裝getEmployees()來獲得員工列表):
public class EmployeeBusinessLayer
{
public List<Employee> GetEmployees()
{
SalesERPDB salesDal = new SalesERPDB();
return salesDal.Employees.ToList(); // Getting an exception in this line.
}
}
Employee類:
public class Employee
{
[Key]
public int EmployeeId { get; set; }
public string FName { get; set; }
public string LName { get; set; }
public int Salary { get; set; }
}
我能擊中同一連接字符串從不同的應用程序。
連接字符串:
<connectionStrings>
<add name="SalesERPDB" connectionString="Data Source=CSCINDAI406933\\SQLEXPRESS;Initial Catalog=SalesERPDB;Integrated Security=True;" providerName="System.Data.SQLClient"></add>
異常詳細信息:
描述: 'System.InvalidOperationException' 類型的異常出現在EntityFramework.dll但在用戶代碼中沒有處理
異常詳細信息:System.InvalidOperationException:實例失敗。 堆棧跟蹤:[InvalidOperationException:實例故障。] 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重試,DbConnectionOptions USEROPTIONS,DbConnectionInternal oldConnection,DbConnectionInternal &連接)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重試,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重試)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(t目標靶,動作2 operation, TInterceptionContext interceptionContext, Action
3執行,動作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操作)190 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute (動作操作)82 System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(的DbConnection SqlConnection的,動作1 act) +194 System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action
1動作)427個 System.Data.Entity.SqlServer.SqlProviderServices.CheckDatabaseExists(SqlConnection的SqlConnection的,可空1 commandTimeout, String databaseName) +86 System.Data.Entity.SqlServer.SqlProviderServices.DbDatabaseExists(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.C allEndDelegate(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 系統。 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的結果)38 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()9644037 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean & completedSynchronously)+155
修復此問題將非常感激。提前致謝。
更新UR與連接字符串 –
@MidhunMundayadan問題更新了問題用的connectionString – Manju