0

I have a class that I first inserting the Seed () method to register the users already have a combo box filled with the necessary and unique Caunties, then their name is the Id itself.

The duplicate key problem happens when I enter a user which in turn has an address, which has the city, the city has relationship with County, to insert (UserManager.CreateAsync () standard Asp.Net MVC design method with implementation of IdentityUser and etc.) I get an error, because instead of taking a AddOrUpdate () it tries to insert a new one with this key.

How do I solve the problem? I need to set for this model always be AddOrUpdate ().

    public class ApplicationUser : IdentityUser
    {
        [Required]
        public virtual Address Address { get; set; }
    }

    public class Address
    {
        [Required]
        public virtual City City { get; set;}
    }

    public class City
    {
        [Required]
        public virtual County County { get; set;}
    }

    public class County
    {
        [Key]
        public string NameId { get; set;}
    }
10
  • What's the difference between AddOrUpdate() and Insert? Are you referring to the same method? What do you mean by Name is the ID? Commented Jan 20, 2016 at 2:36
  • @KosalaW Insert will insert only. AddOrUpdate makes the existence check if there is updated, otherwise inserts the new record. Commented Jan 20, 2016 at 2:37
  • That's code duplication. You only need AddOrUpdate method. Insert is not required at all. Commented Jan 20, 2016 at 2:38
  • @KosalaW Yes, it's what I say in the question, I can not insert a new one.But I do not know how to send only update the County because the county is a City property that is an Address property that is owned by a user. Commented Jan 20, 2016 at 2:40
  • @KosalaW see again the question, insert an example of code Commented Jan 20, 2016 at 2:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.