【问题标题】:Entity framework table creating c#实体框架表创建c#
【发布时间】:2017-04-03 05:42:47
【问题描述】:

我有一个名为 SalesERPDB 的数据库,我正在尝试创建一个新表“TblEmployee”,但在创建时遇到了异常。

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.
    }
}

员工类:

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>

异常详情:

描述:EntityFramework.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理

异常详情:System.InvalidOperationException:实例失败。 堆栈跟踪:[InvalidOperationException:实例失败。] System.Web.Mvc.Async.c__DisplayClass46.b__3f() +323 System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +78 System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 重试,DbConnectionOptions userOptions,DbConnectionInternal oldConnection,DbConnectionInternal& 连接)+191 System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection externalConnection,DbConnectionFactory connectionFactory,TaskCompletionSource1 retry, DbConnectionOptions userOptions) +119 System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +44 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult)+47 System.Web.Mvc.Async.WrappedAsyncResultBase1.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, TaskCompletionSource1 重试,DbConnectionOptions userOptions) +154 System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) +21 System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) +141 System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 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.InternalDispatcher1.Dispatch(TTarget 目标,Action2 operation, TInterceptionContext interceptionContext, Action3 正在执行,Action3 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(Func1 操作) +190 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(动作操作)+82 System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection,Action1 act) +194 System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action1 行为)+427 System.Data.Entity.SqlServer.SqlProviderServices.CheckDatabaseExists(SqlConnection sqlConnection, Nullable1 commandTimeout, String databaseName) +86 System.Data.Entity.SqlServer.SqlProviderServices.DbDatabaseExists(DbConnection connection, Nullable1 commandTimeout, Lazy1 storeItemCollection) +347 System.Data.Entity.Core.Common.DbProviderServices.DatabaseExists(DbConnection connection, Nullable1 commandTimeout, Lazy1 storeItemCollection) +122 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +42 System.Web.Mvc.Async.WrappedAsyncResultBase1.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.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +70 System.Web.Mvc.Async.WrappedAsyncResultBase1.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.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +62 System.Web.Mvc.Async.WrappedAsyncResultBase1.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.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +70 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, 对象标签) +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

非常感谢修复此问题。提前致谢。

【问题讨论】:

  • 用连接字符串更新你的问题
  • @MidhunMundayadan 用 connectionString 更新了问题

标签: c# asp.net-mvc entity-framework


【解决方案1】:

当您收到基于堆栈跟踪的错误“实例失败”时,这可能是您的 SQL Server 实例的错误。更改数据源,如Data Source=.\SQLEXPRESS

【讨论】:

  • 是的,问题出在我的服务器实例上,将数据源值从 \\SQLEXPRESS 更新为 \SQLEXPRESS。它奏效了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 1970-01-01
  • 2016-02-05
相关资源
最近更新 更多