出现此问题的原因是 Application_Start() 函数仅在第一个用户操作时触发。但是在用户可以事件执行操作之前触发 IndexController。只需从您的项目中删除 [InitializeSimpleMembership],它也给了我很多挫败感......
只需将以下代码添加到您的 global.asax
private static SimpleMembershipInitializer _initializer;
private static object _initializerLock = new object();
private static bool _isInitialized;
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
}
public class SimpleMembershipInitializer
{
public SimpleMembershipInitializer()
{
using (var context = new UsersContext())
context.UserProfiles.Find(1);
if (!WebSecurity.Initialized)
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
}
}
当然,请确保您创建了正确的 dbContext() 并设置了与您的项目匹配的正确 InitializeDatabaseConnections 属性。
氪