请帮我解决这个问题。
我编写了一个代码,用于使用LINQ
从数据库中获取第一条记录My Homecontroler
[CustomHandleError]
public class HomeController : Controller
{
Domain domain;
public HomeController()
{
domain = new Domain();
}
public ActionResult Index()
{
HomePageViewModel vm = new HomePageViewModel();
vm.Page = domain.GetHomePageInfo();
if (vm.Page != null)
{
ViewBag.Title = vm.Page.PageTitle;
ViewBag.Keywords = vm.Page.PageKeywords;
ViewBag.Description = vm.Page.PageDesc;
ViewBag.PageImage = vm.Page.PageTabImage;
ViewBag.ColorCode = vm.LatestNewsLetter.IssueColorCode;
ViewBag.emptyItemCount = emptyItemCount;
return View(vm.Page.PageTemplate, vm);
}
return View(vm);
}
}
DataAccess Method
#region Front End
public Page GetHomePageInfo()
{
try
{
Page pagedata = new Page();
pagedata = context.Page.Where(x => x.PageIsHomePage == true && x.PageActive==true && x.PageVisible==true).FirstOrDefault();
return pagedata;
}
catch (Exception ex)
{
CustomFilters.logerrortextfile.errorlog(ex, "GetHomePageInfo");
throw ex;
}
}
Web config
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</configSections>
<connectionStrings>
<add name="dbContext" connectionString="Data Source=sqlcluster1;Initial Catalog=abc_Mvc;User ID=abc_Mvc;Password=abcd_Mvc(&" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation targetFramework="4.5" debug="true"/>
<httpRuntime targetFramework="4.5" maxRequestLength="2147483647"/>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880"/>
</authentication>
<customErrors mode="On" defaultRedirect="~/Error/General"/>
<!--<customErrors mode="RemoteOnly" ></customErrors>-->
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483647"/>
</requestFiltering>
</security>
<!--<staticContent>
<mimeMap fileExtension=".mp4" mimeType="video/mp4" />
</staticContent>-->
</system.webServer>
它正在本地机器上工作。当我在服务器上发布时。我不经常收到错误,但有时候,有人请帮助我。我使用实体框架与代码优先的方法。
错误消息:DataException
异常类型:System.Data.DataException
错误位置:初始化数据库时发生异常。有关详细信息,请参阅InnerException 来源:EntityFramework堆栈跟踪:
在System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
在System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
在System.Data.Entity.Internal.LazyInternalContext.b__4(InternalContext c)
在System.Data.Entity.Internal.RetryAction1.PerformAction(TInput input)
1动作)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action
在System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
在System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
在System.Data.Entity.Internal.Linq.InternalSet1.Initialize()
1.get_InternalContext()
at System.Data.Entity.Internal.Linq.InternalSet
在System.Data.Entity.Infrastructure.DbQuery1.System.Linq.IQueryable.get_Provider()
1源,表达式`1谓词)
at System.Linq.Queryable.Where[TSource](IQueryable
在DomainLogic.Domain.GetHomePageInfo()
内部异常
source :EntityFramework
Inner Exception: System.Data.Entity.Core.EntityException: The underlying provider failed on Open. ---> System.Data.SqlClient.SqlException: Cannot open database "abc_Mvc" requested by the login. The login failed.
Login failed for user 'abc_Mvc'.
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<Open>b__36(DbConnection t, DbConnectionInterceptionContext c)
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
at System.Data.Entity.Core.EntityClient.EntityConnection.<Open>b__2()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
--- End of inner exception stack trace ---
at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection(Boolean shouldMonitorTransactions)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable`1 sequence)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
at System.Data.Entity.Internal.EdmMetadataRepository.QueryForModelHash(Func`2 createContext)
at System.Data.Entity.Internal.InternalContext.QueryForModelHash()
at System.Data.Entity.Internal.ModelCompatibilityChecker.CompatibleWithModel(InternalContext internalContext, ModelHashCalculator modelHashCalculator, Boolean throwIfNoMetadata, DatabaseExistenceState existenceState)
at System.Data.Entity.Internal.InternalContext.CompatibleWithModel(Boolean throwIfNoMetadata, DatabaseExistenceState existenceState)
at System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context)
at System.Data.Entity.Internal.InternalContext.<>c__DisplayClassf`1.<CreateInitializationAction>b__e()
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)