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;}
    }
Luiz Negrini
  • 377
  • 1
  • 2
  • 14
  • What's the difference between AddOrUpdate() and Insert? Are you referring to the same method? What do you mean by _Name is the ID_? – Kosala W Jan 20 '16 at 02:36
  • @KosalaW Insert will insert only. AddOrUpdate makes the existence check if there is updated, otherwise inserts the new record. – Luiz Negrini Jan 20 '16 at 02:37
  • That's code duplication. You only need `AddOrUpdate` method. Insert is not required at all. – Kosala W Jan 20 '16 at 02: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. – Luiz Negrini Jan 20 '16 at 02:40
  • @KosalaW see again the question, insert an example of code – Luiz Negrini Jan 20 '16 at 02:43
  • What's that code supposed to mean? You have an `Address` class that has a property of type `City`. Which part of the world has only a "City" in a Address? :). Think again. – Kosala W Jan 20 '16 at 02:48
  • @KosalaW the models are correct. the user has an address that belongs to a city, belonging to a county. – Luiz Negrini Jan 20 '16 at 02:53
  • That's not correct. Address should have things like, street number, address line1, address line 2, City, County/State, Zip/postcode etc. If your "Address" doesn't have them, you should call it something else. By the way, this code is not sufficient to understand your issue. Show us AddUpdate() method. – Kosala W Jan 20 '16 at 03:15
  • Take a look at [this](http://stackoverflow.com/a/15413951/3881866) – John Ephraim Tugado Jan 20 '16 at 03:40
  • I did not say I had a addOrUpdate method of execution.I said I have in my SEED method that is where it enters a certain code when running the Update-Database on the island. Then when I create a user who has this part address (in which the seed has entered me the County of data, obviously that can not be changed) the duplicate key error. – Luiz Negrini Jan 20 '16 at 21:57

0 Answers0