Bobby Tables
Bobby Tables

Reputation: 3013

Seeding Membership with custom data Entity framework

I have the fallowing Models

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; }
    }
}

And one that inherits it

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; }
    }
}

And the seed method

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

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 = "[email protected]",
            FirstName = firstName,
            LastName = lastName
        };
        _context.Users.Add(profile);
        _context.SaveChanges();
        WebSecurity.CreateAccount(user, password);
    }
}

But now i get: - An error occurred while updating the entries. See the inner exception for details. System.Data.SqlClient.SqlException: Cannot insert explicit value for identity column in table 'UserProfile' when IDENTITY_INSERT is set to OFF.

Upvotes: 1

Views: 214

Answers (1)

AlexC
AlexC

Reputation: 10756

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);

Notice the use of the CreateAccount rather than the CreateUserAndAccount method

Upvotes: 2

Related Questions