我打开了 Asp.Net Mvc 4 Internet App。模板。我为我的数据库设置连接字符串,如下所示:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=ALI-PC\;Initial Catalog=OsosPlusDb;Persist Security Info=True;User ID=sa;Password=sa;" providerName="System.Data.SqlClient" />
</connectionStrings>
我运行项目,单击注册按钮。我创建了一个这样的新用户:
当我单击注册发布时,我收到此错误:
但它在 DB 中创建了一个新用户并且它不创建membership_user(会员表不包含添加的用户信息):
我找不到什么问题。有时没有错误发生。
更新
和异常快照:
账户型号:
public class UsersContext : DbContext
{
public UsersContext()
: base("DefaultConnection")
{
}
public DbSet<UserProfile> UserProfiles { get; set; }
}
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
}
InitializeSimpleMembershipAttribute.cs
[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(ActionExecutingContext filterContext)
{
// 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<UsersContext>(null);
try
{
using (var context = new UsersContext())
{
if (!context.Database.Exists())
{
// Create the SimpleMembership database without Entity Framework migration schema
((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
}
}
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
}
catch (Exception ex)
{
throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
}
}
}
}
最后的编辑和总结
问题是字符(“i”)。如果我不使用“i”,一切都会好起来的。但是当我使用它时,我得到了那个错误。我该如何解决这个问题。