种子成员与自定义数据实体框架 [英] Seeding Membership with custom data Entity framework

查看:145
本文介绍了种子成员与自定义数据实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有休闲模型

namespace Prometheus.Models
{
    [Table("People")]
    public class Person : IPerson
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string Name
        {
            get
            {
                return FirstName + " " + LastName;
            }
            set{}
        }

        public string Email { get; set; }
        public DateTime? LastModified { get; set; }
    }
}

继承它的人

namespace Prometheus.Models
{
    [Table("UserProfile")]
    public class UserProfile : Person
    {
        public string UserName { get; set; }
        public string CNP { get; set; }
        public virtual Faculty Faculty { get; set; }
        public bool? IsUSAMV { get; set; }
        public virtual ICollection<Result> Results { get; set; }
        public virtual ICollection<Project> Projects { get; set; }
    }
}

而种子方法

private void AddUser(string user, string password,
    SimpleMembershipProvider membership)
{
    if (membership.GetUser(user, false) == null)
    {
        WebSecurity.CreateUserAndAccount(user, password, new
        {
            CNP = "1890531111111",
            IsUSAMV = true,
        });
    }
}

当我尝试运行没有UserProfile扩展的种子方法人的一切都可以,但是当它延伸时,我会继续收到休眠错误。

When i try to run the seed method without UserProfile extending Person everything is ok, but when it extends it i keep getting the fallowing error.

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.UserProfile_dbo.People_Id". The conflict occurred in database "PrometheusDb", table "dbo.People", column 'Id'.
The statement has been terminated.

任何帮助都将非常感谢。

Any help would be greatly appreciated thanks.

我尝试将我的功能更新为

I tried updateing my function to

private void AddUser(string firstName,string lastName, string password,
    SimpleMembershipProvider membership)
{
    var user = firstName + "." + lastName;
    if (membership.GetUser(user, false) == null)
    {
        var profile = new UserProfile()
        {
            Email = "test@email.com",
            FirstName = firstName,
            LastName = lastName
        };
        _context.Users.Add(profile);
        _context.SaveChanges();
        WebSecurity.CreateAccount(user, password);
    }
}

但现在我得到:
- strong>更新条目时出错。查看内部例外情况。
System.Data.SqlClient.SqlException:当IDENTITY_INSERT设置为OFF时,不能在表'UserProfile'中为identity列插入显式值。

推荐答案

您在创建项目的顺序和数据库的完整性检查时遇到问题。我做了类似的事情,它涉及到先保存用户帐户。我的代码如下:

You are having a problem in the order that items are created and the integrity checks on the db. I have done something similar and it has involved saving the user first then the account. My code looks like:

var user = new User { UserName = model.UserName, Organisation = userOrg };
this.repository.SaveUser(user);
string token = WebSecurity.CreateAccount(model.UserName, model.Password, true);

注意使用 CreateAccount 而不是 CreateUserAndAccount 方法

这篇关于种子成员与自定义数据实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆