![](/img/trans.png)
[英]Why am I getting a compile error when I'm trying to create a list of a dynamic object at runtime?
[英]Why am I getting an Invalid Object Name error when trying to create a user?
我有以下代碼嘗試通過新的asp.net Identity 2.0創建默認用戶,如果實際上當我的Web應用程序第一次運行時用戶尚不存在:
public class DotNetIdentity
{
// PROPERTIES
public UserManager<ApplicationUser, string> UserManager { get; private set; }
public RoleManager<IdentityRole> RoleManagement { get; private set; }
// CONSTRUCTORS
public DotNetIdentity()
{
// create the user manager
UserManager = new UserManager<ApplicationUser, string>(new UserStore<ApplicationUser, CustomRole, string, CustomUserLogin, CustomUserRole,
CustomUserClaim>(
new myDbContext()));
#region Define user manager settings
// Configure validation logic for usernames
UserManager.UserValidator = new UserValidator<ApplicationUser, string>
(
UserManager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
UserManager.PasswordValidator = new PasswordValidator()
{
RequiredLength = 6,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = false,
RequireUppercase = false
};
// Configure user lockout defaults
UserManager.UserLockoutEnabledByDefault = true;
UserManager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
UserManager.MaxFailedAccessAttemptsBeforeLockout = 15;
// Register two factor authentication providers.
UserManager.RegisterTwoFactorProvider
("PhoneCode", new PhoneNumberTokenProvider<ApplicationUser, string>()
{
MessageFormat = "Your security code is: {0}"
});
UserManager.RegisterTwoFactorProvider
("EmailCode", new EmailTokenProvider<ApplicationUser, string>()
{
Subject = "SecurityCode",
BodyFormat = "Your security code is {0}"
});
UserManager.EmailService = new EmailService();
UserManager.SmsService = new SmsService();
#endregion
// create the role manager
RoleManagement = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(
new myDbContext()));
}
public void CreateDefaultAdministrator()
{
// db connection
using (var db = new myDbContext())
{
// create admin obj
var admin = new ApplicationUser()
{
Id = Guid.NewGuid().ToString(),
UserName = "Dev",
Email = "dev@blahblah.com",
IsActive = true
};
// create user
var result = UserManager.Create(admin, "Blahblah*0");
// check if role exist
if (!RoleManagement.RoleExists("Administrator"))
{
// Add Administrator Role
var createResult = RoleManagement.Create(
new IdentityRole("Administrator"));
}
if (!RoleManagement.RoleExists("Developer"))
{
// Add Developer Role
var createResult = RoleManagement.Create(
new IdentityRole("Developer"));
}
// add user to roles
var createdUser = UserManager.FindByName("Dev");
if (createdUser != null)
{
UserManager.AddToRole(createdUser.Id, "Administrator");
UserManager.AddToRole(createdUser.Id, "Developer");
}
}
}
}
當上面的代碼運行時,我得到錯誤無效的對象名稱'dbo.ApplicationUsers'。 代碼與之通信的數據庫是空的,因此我希望此代碼生成所需的表。 我使用asp.net Identity 1.0獲得了類似的代碼,完美無缺。 我錯過了什么或可能導致此錯誤的原因。 下面是ApplicationUser和相關類的代碼:
public class CustomRole : IdentityRole<string, CustomUserRole>
{
public CustomRole() { }
public CustomRole(string name)
{
Id = Guid.NewGuid().ToString();
Name = name;
}
}
public class CustomUserRole : IdentityUserRole<string>
{
public string Id { get; set; }
public CustomUserRole()
{
Id = Guid.NewGuid().ToString();
}
}
public class CustomUserClaim : IdentityUserClaim<string> { }
public class CustomUserLogin : IdentityUserLogin<string>
{
public string Id { get; set; }
public CustomUserLogin()
{
Id = Guid.NewGuid().ToString();
}
}
// define the application user
public class ApplicationUser : IdentityUser<string, CustomUserLogin, CustomUserRole,
CustomUserClaim>
{
[Required]
public bool IsActive { get; set; }
public ApplicationUser()
{
}
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, string> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public partial class myDbContext : IdentityDbContext<ApplicationUser, CustomRole, string,
CustomUserLogin, CustomUserRole, CustomUserClaim>
{
static myDbContext()
{
Database.SetInitializer<myDbContext>(null);
}
public myDbContext()
: base("Name=myDbContext")
{
}
public DbSet<TestTable> TestTables { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new TestTableMap());
}
}
}
我必須為我的項目啟用遷移。 這解決了我的問題。 我用這篇文章來幫助我。 http://www.dotnet-tricks.com/Tutorial/entityframework/R54K181213-Understanding-Entity-Framework-Code-First-Migrations.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.