我正在使用Entity Framework构建我的数据库,当我在localhost上运行它时,它可以工作。
不幸的是,当我发布到我的webhost服务器时,我无法初始化数据库,因为他们拒绝授予SQL用户删除或创建数据库的权限,因此我的初始化程序看起来像:
Database.SetInitializer<OrtundEntities>(null);
通常情况下,MVC项目最好将一些默认数据植入数据库(例如用户角色,用户等),这样我就可以使用登录来测试并最终管理我的数据。
我也必须禁用它,所以我的global.asax.cs文件如下所示:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
OrtundEntities db = new OrtundEntities();
db.Database.Initialize(true);
// add default user accounts and other default data into the database
//db.Seed();
}
不幸的是,当我尝试在线访问该网站的URL时,我收到以下错误。
错误文字:
无效的对象名称'dbo.UserRoles'
我将此模型的名称从Roles
更改为UserRoles
,因为我认为第一个名称可能与SQL关键字冲突。我的DbSets如下:
public DbSet<POSProvider> POSProviders { get; set; }
public DbSet<Receipt> Receipts { get; set; }
public DbSet<Reward> Rewards { get; set; }
public DbSet<UserRole> UserRoles { get; set; }
public DbSet<Store> Stores { get; set; }
public DbSet<User> Users { get; set; }
UserRole模型看起来像这样(所有这些都是精确的复制/粘贴)
public class UserRole : GenericServiceModel
{
public string RoleName { get; set; }
}
GenericServiceModel看起来像这样(我会在其中添加更多内容,因为它可能有意义:
public class GenericServiceModel
{
public int Id { get; set; }
public bool Deleted { get; set; }
}
堆栈跟踪
[SqlException(0x80131904):无效的对象名称'dbo.UserRoles'。]
System.Data.SqlClient.SqlConnection.OnError(SqlException异常, Boolean breakConnection,Action1 wrapCloseInAction) +388
1完成,Int32超时,Task&amp;任务, Boolean asyncWrite)+586
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +717
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +4515
System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +61
System.Data.SqlClient.SqlDataReader.get_MetaData() +134
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +6557689
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) +6560327
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource
System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String 方法)+104
System.Data.SqlClient.SqlCommand.ExecuteReader(的CommandBehavior 行为,字符串方法)+288
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(的CommandBehavior 行为)+171
System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch(TTarget target, Func
3操作,TInterceptionContext interceptionContext, 行动3 executing, Action
3已执行)+208
System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(的DbCommand command,DbCommandInterceptionContext interceptionContext)+438
System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,CommandBehavior行为)+53[EntityCommandExecutionException:执行时发生错误 命令定义。有关详细信息,请参阅内部异常。]
System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,CommandBehavior行为)+155
System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute(ObjectContext的 context,ObjectParameterCollection parameterValues)+1117
System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func键1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) +516
1 操作)+263
System.Data.Entity.Core.Objects.<>c__DisplayClass7.<GetResults>b__5() +170 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func
System.Data.Entity.Core.Objects.ObjectQuery1.GetResults(Nullable
1 forMergeOption)+268
System.Data.Entity.Core.Objects.ObjectQuery1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() +11 System.Data.Entity.Internal.LazyEnumerator
1.MoveNext()+50 System.Linq.Enumerable.Single(IEnumerable1 source) +188
1 source)+298
System.Linq.Queryable.Any(IQueryable
OrtundService.Models.OrtundEntities.Seed()+649
OrtundService.MvcApplication.Application_Start()+ 382[HttpException(0x80004005):执行时出错 命令定义。有关详细信息,请参阅内部异常。]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext的 上下文,HttpApplication app)+12601645
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr的 appContext,HttpContext上下文,MethodInfo []处理程序)+175
System.Web.HttpApplication.InitSpecial(HttpApplicationState状态, MethodInfo [] handlers,IntPtr appContext,HttpContext context)+304
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr的 appContext,HttpContext context)+404
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr的 appContext)+475[HttpException(0x80004005):执行时出错 命令定义。有关详细信息,请参阅内部异常。]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context)+12618692 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +159 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest) wr,HttpContext context)+12458309
我在数据模型中甚至在生成的数据库中都看不到任何问题,所以我没有任何线索可以去哪里或者做什么来解决问题。
我在我的开发服务器上执行了以下查询:
SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, COLUMN_DEFAULT
FROM ortund.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'UserRoles';
GO
它给了我以下结果:
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME COLUMN_DEFAULT
ortund dbo UserRoles Id NULL
ortund dbo UserRoles RoleName NULL
ortund dbo UserRoles Deleted NULL
谁能告诉我这里发生了什么?错误来自哪里?
修改
我的用户模型将UserRole链接到2个表之间的结构化关系:
public class User : GenericServiceModel
{
public UserRole Role { get; set; }
public string EmailAddress { get; set; }
public string Password { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public decimal RewardTotal { get; set; }
}