Entity Framework Cannot insert duplicate key in object

asp.net-mvc asp.net-mvc-5 asp.net-web-api c# entity-framework-6

Question

Can not find solution to my problem. I'm sorry, if I duplicate the Question.

I have relation in Entity Framework code first one to many:

[DataContract(IsReference = true)]
public class PricingPlan
{

    public PricingPlan()
    {
        UserProfiles = new List<UserProfile>();
    }

    [Key]
    [DataMember]
    public Guid PricingPlanId { get; set; }

    [DataMember]
    public string Type { get; set; }

    public ICollection<UserProfile> UserProfiles { get; set; }
}

[DataContract(IsReference = true)]
public class UserProfile : IdentityUser
{
    public DateTime JoinedOn { get; set; }
    public DateTime LastVisited { get; set; }
    public string Location { get; set; }

    public Guid PricingPlanId { get; set; }
    public PricingPlan PricingPlan { get; set; }
}

In PricingPlan Table I have 2 rows Free and Pro

Whe user register, I want to add him to Free plan:

    [AllowAnonymous]
    [Route("Register")]
    public async Task<IHttpActionResult> Register(RegisterBindingModel model)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        PricingPlan pp = UnitOfwork.PricingPlanRepository.FirstOrDefault(v => v.Type == "Free");

        UserProfile user = new UserProfile
        {
            UserName = model.UserName,
            Email = model.Email,
            EmailConfirmed = false,
            Location = model.Location,
            JoinedOn = DateTime.Now,
            LastVisited = DateTime.Now,
            PricingPlanId = pp.PricingPlanId,
            PricingPlan = pp
        };

        IdentityResult identityResult = await UserManager.CreateAsync(user, model.Password);

I'm taking existing plan from Db and adding it to userProfile property, but when UserManager trying creat user, I'm getting exception:

Violation of PRIMARY KEY constraint 'PK_dbo.PricingPlans'. Cannot insert duplicate key in object 'dbo.PricingPlans'. The duplicate key value is (a5139db8-64c1-49a2-97ef-55009259dc23).\r\nThe statement has been terminated."

OnModelCreating I have this code:

  modelBuilder.Entity<UserProfile>().HasRequired<PricingPlan>(r=>r.PricingPlan).WithMany(c => c.UserProfiles).HasForeignKey(a=>a.PricingPlanId);

What is the right way to add existing entity to new entity in one to many relation in entity framework code first?

1
3
11/10/2014 11:28:18 AM

Accepted Answer

Ok, I solved the problem.

I removed PricingPlan = pp, like markpsmith said. Then, when I wan to get UserProfile with attached entity Pricingplan in it, I just make Include:

this.Context.Set<UserProfile>().Include("PricingPlan").

But can not understand, why it is not included by default, when LazyLoading is set to false.

4
1/29/2018 11:18:20 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow