simplemember - object 'Users'已经存在
本文关键字:存在 Users simplemember object | 更新日期: 2023-09-27 18:07:43
我正在用Asp.net MVC开发SPA。项目工作在本地主机很好,但我有一个问题的主机。
有一个控制器可以与包含少量方法的MembershipProvider
一起工作。
[InitializeSimpleMembership]
public class AccountController : ApiController
{
public object Get()
{
...
}
...
}
关键时刻-是属性InitializeSimpleMembership
。声明:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class InitializeSimpleMembershipAttribute : ActionFilterAttribute
{
private static SimpleMembershipInitializer _initializer;
private static object _initializerLock = new object();
private static bool _isInitialized;
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
// Ensure ASP.NET Simple Membership is initialized only once per app start
LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
}
private class SimpleMembershipInitializer
{
public SimpleMembershipInitializer()
{
Database.SetInitializer(new ITAInitializer());
using (var context = new ITAContext())
{
if (!context.Database.Exists())
{
((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
}
}
WebSecurity.InitializeDatabaseConnection("ITAContext", "Users", "ID", "Login", autoCreateTables: true);
if (!Roles.RoleExists(UserRights.Admin.ToString()))
Roles.CreateRole(UserRights.Admin.ToString());
if (!Roles.RoleExists(UserRights.Moderator.ToString()))
Roles.CreateRole(UserRights.Moderator.ToString());
if (!Roles.RoleExists(UserRights.User.ToString()))
Roles.CreateRole(UserRights.User.ToString());
}
}
}
当我在控制器中调用任何方法时,我得到一个错误:
数据库中已经存在一个名为"Users"的对象。
如果我要删除表Users
,那么首先请求运行良好并创建表。在下一个呼叫中,我再次得到一个错误。为什么它试图创建表,而不是使用现有的?
- 从控制器中删除此属性
-
在某些类中添加
WebSecurity.InitializeDatabaseConnection
方法:public static class AuthConfig { public static void RegisterAuth() { WebSecurity.InitializeDatabaseConnection("ITAContext", "Users", "ID", "Login", autoCreateTables: true); if (!Roles.RoleExists(UserRights.Admin.ToString())) Roles.CreateRole(UserRights.Admin.ToString()); if (!Roles.RoleExists(UserRights.Moderator.ToString())) Roles.CreateRole(UserRights.Moderator.ToString()); if (!Roles.RoleExists(UserRights.User.ToString())) Roles.CreateRole(UserRights.User.ToString()); } }
-
调用这个方法:
public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AuthConfig.RegisterAuth(); } }
数据库用户权限问题。添加VIEW DEFINITION
和ALTER
后,我的代码工作正常