如何使用实体框架迁移在我的用户表中创建用户帐户?
以前,在使用 Code First 创建表时,我使用数据库初始值设定项来播种或预填充表。这一切都很有效,但我想继续使用 EF 4.3 迁移。
我在项目中更新到 EF 4.3,并删除了数据库初始化程序代码,并将其放在迁移 Configuration.cs 文件的 Seed 方法中。对于我来说,这对于预填充我的表来说一切正常,除非我想将默认用户播种到我的用户表中。
我在我的 Seed 方法中使用以下代码:(目前,这不使用 AddOrUpdate 代码,因为我不确定如何在这种情况下使用它 - 因此需要检查用户名)
// create default User
MembershipCreateStatus status = new MembershipCreateStatus();
User admin = context.Users.Find("TestGuy");
if (admin == null)
{
Membership.CreateUser("TestGuy", "TestGuy123", "[email protected]");
if (status == MembershipCreateStatus.Success)
{
admin.Firstname = "Test";
admin.Surname = "Guy";
admin.DateLastActivity = DateTime.Now;
admin.LastActivityRoute = "/Home/Index";
Role adminRole = context.Roles.Find("Administrator");
admin.Roles = new List<Role>();
admin.Roles.Add(adminRole);
}
}
当我尝试运行 Update-Database 时,我得到了这个错误:
The password-answer supplied is invalid.
我认为这取决于我如何调用 CreateUser,但我似乎无法弄清楚如何解决这个错误。我尝试在安全问题和答案中只输入空值,但这似乎不起作用。
有没有人有使用“AddOrUpdate”选项为用户播种的示例?
编辑 1:
根据下面的评论之一,我正在使用 Altairis Security Toolkit 来管理我的会员资格。我的 web.config 设置如下:
<membership defaultProvider="TableMembershipProvider">
<providers>
<clear />
<add name="TableMembershipProvider" type="Altairis.Web.Security.TableMembershipProvider, Altairis.Web.Security" connectionStringName="DataContext" minRequiredPasswordLength="8" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" minRequiredNonAlphanumericCharacters="0" passwordStrengthRegularExpression="" />
</providers>
</membership>
编辑 2:
我还将其作为开放票证放在 Altairis 论坛上。 http://altairiswebsecurity.codeplex.com/workitem/32273
编辑3:
我必须按如下方式设置我的代码:
// create default User
MembershipCreateStatus status = new MembershipCreateStatus();
User admin = context.Users.Find("TestGuy");
if (admin == null)
{
Membership.CreateUser("TestGuy", "TestGuy123", "[email protected]", null, null, true, out status);
if (status == MembershipCreateStatus.Success)
{
admin = context.Users.Find("TestGuy");
admin.Firstname = "Test";
admin.Surname = "Guy";
admin.DateLastActivity = DateTime.Now;
admin.LastActivityRoute = "/Home/Index";
Role adminRole = context.Roles.Find("Administrator");
admin.Roles = new List<Role>();
admin.Roles.Add(adminRole);
}
}
上面的代码将在运行 Update-Database 时编译,但不会生成新用户。如果有人有尝试的建议那就太好了!
谢谢,
丰富
Previously I used a database initializer to seed or pre-populate my tables when creating them using Code First. This all worked great but I wanted to move on to using the EF 4.3 migrations.
I updated to EF 4.3 in my project and removed the database initializer code and put it within the Seed method on the migrations Configuration.cs file. This is all working ok for me for prepopulating my tables except for when I want to seed a default user into my user table.
I use the following code within my Seed method: (currently this doesn't use the AddOrUpdate code as I'm not sure how to use that for this situation - hence the username check)
// create default User
MembershipCreateStatus status = new MembershipCreateStatus();
User admin = context.Users.Find("TestGuy");
if (admin == null)
{
Membership.CreateUser("TestGuy", "TestGuy123", "[email protected]");
if (status == MembershipCreateStatus.Success)
{
admin.Firstname = "Test";
admin.Surname = "Guy";
admin.DateLastActivity = DateTime.Now;
admin.LastActivityRoute = "/Home/Index";
Role adminRole = context.Roles.Find("Administrator");
admin.Roles = new List<Role>();
admin.Roles.Add(adminRole);
}
}
When I try and run Update-Database I get this error:
The password-answer supplied is invalid.
I think this is just down to how I call the CreateUser but I can't seem to work out how to get around this bug. I've tried putting in just nulls for the security question and answer but that doesn't seem to work.
Has anyone an example of seeding a user using the 'AddOrUpdate' option?
EDIT 1:
As per one of the comments below, I am using Altairis Security Toolkit to manage my membership. My web.config is setup as follows:
<membership defaultProvider="TableMembershipProvider">
<providers>
<clear />
<add name="TableMembershipProvider" type="Altairis.Web.Security.TableMembershipProvider, Altairis.Web.Security" connectionStringName="DataContext" minRequiredPasswordLength="8" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" minRequiredNonAlphanumericCharacters="0" passwordStrengthRegularExpression="" />
</providers>
</membership>
EDIT 2:
I have also put this as an open ticket on the Altairis forum.
http://altairiswebsecurity.codeplex.com/workitem/32273
EDIT 3:
To get this working without any error I had to setup my code as follows:
// create default User
MembershipCreateStatus status = new MembershipCreateStatus();
User admin = context.Users.Find("TestGuy");
if (admin == null)
{
Membership.CreateUser("TestGuy", "TestGuy123", "[email protected]", null, null, true, out status);
if (status == MembershipCreateStatus.Success)
{
admin = context.Users.Find("TestGuy");
admin.Firstname = "Test";
admin.Surname = "Guy";
admin.DateLastActivity = DateTime.Now;
admin.LastActivityRoute = "/Home/Index";
Role adminRole = context.Roles.Find("Administrator");
admin.Roles = new List<Role>();
admin.Roles.Add(adminRole);
}
}
The above will compile now when running Update-Database but no new user is generated. If anyone has suggestions to try that would be great!
Thanks,
Rich
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的会员资格需要问答。您是否尝试过使用“if (Membership.RequiresQuestionAndAnswer)”进行检查并查看它的分支方式。
致管理员:顺便说一句,我想先将其作为评论发布,因为上次使用答案时被标记,但我在这里没有看到任何用于添加评论的选项,只有答案。
I think your Membership is set to require question and answer. Have you tried putting a check in using "if (Membership.RequiresQuestionAndAnswer)" and see which way it branches.
To Admin: By the way I wanted to post this first as a comment as I was flagged last time when using an answer, but I don't see any option here for adding comments, only answers.
通过对这个问题进行一些研究,很明显,用户播种应该在迁移种子逻辑之外创建 - 至少现在是这样,直到它们变得更容易做到!
作为解决方法,我遵循了此 Stackoverflow 问题中的示例(在实体框架 4.3 中增量播种数据的最佳方法),以手动使我的新用户在迁移配置文件之外启动。
至少对我来说,这并不理想,因为所有“启动”数据都不在一个区域,但它可以完成工作,并且并不难遵循。
From doing some research on this issue it's become apparent that the user seeding should be created outside the migrations seed logic - at least for now until they make it easier to do!
As a work around I followed the example from this Stackoverflow question (Best way to incrementally seed data in Entity Framework 4.3) to manually make my new user on start up outside of the migrations configuration file.
It's not ideal, at least to me, as all 'startup' data is not in one area but it gets the job done and isn't that difficult to follow.