locked
Storing multiple physical addresses against Asp.net Identity RRS feed

  • Question

  • User269881539 posted

    I had extended the Identity provider with physical (postal) address etc. However I'd like to actually store multiple physical addresses for each user.

    In my DB I have a table called AccountAddress :

    ID int (IDENTITY 
    AccountID int 
    CompanyName varchar(100) 
    Address1 varchar(100) 
    Address2 varchar(100) 
    Town varchar(100) 
    County varchar(100) 
    Postcode varchar(20) 
    Country varchar(50) 
    AddressCreated datetime 

    My ApplicationUser :

            //extended properties
            //decorate for IDENTITY column
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int AccountId { get; set; }
    	public string Firstname { get; set; }
            public string Lastname { get; set; }
            public string TelNo { get; set; }
            public string MobileNo { get; set; }
            public DateTime AccountCreated { get; set; }
            public DateTime AccountUpdated { get; set; }
            public ICollection<AccountAddress> Addresses { get; set; }

    My AccountAddress class :

        [Table("AccountAddress")]
        public class AccountAddress : Address
        {
            [Key]
            public int ID { get; set; }
            public ApplicationUser ApplicationUser { get; set; }
        }

    My Address class :

        public class Address
        {
            public string Company { get; set; }
            public string Address1 { get; set; }
            public string Address2 { get; set; }
            public string Town { get; set; }
            public string County { get; set; }
            public string Postcode { get; set; }
            public string Country { get; set; }
        }
    

    How do I get this to hook up automatically so I can retrieve / store multiple addresses using the Identity provider code? Or am I better off putting all this in my own code and retrieving this data myself?

    Friday, October 7, 2016 1:56 PM

All replies

  • User-471420332 posted

    Dear chilluk, 

                   You can insert address1 and address2 in different columns and also you can use Arithmetic operator to combine both address1 and address2 for displaying.

    Thank you

    Friday, October 7, 2016 2:26 PM
  • User269881539 posted

    Sorry I mean more how do I get this to work with respect actually getting the data in and out of the database? With my own code in other areas I'm used to having my controller call a manager class that in turn calls my DAL to fetch data from the db - with the identity provider code how / where do I tell it to "OK grab me the addresses for this user now please"?

    Friday, October 7, 2016 2:29 PM
  • User-471420332 posted

    Dear chilluk,

                 If you post that much of code then you will get that much of answer only. Please expand you question with full details and some extra code.

    Thank you

    Friday, October 7, 2016 2:33 PM
  • User269881539 posted

    What else could / should I post - it's the .Net Identity provider which I just use in my controller such :

    //if logged in fetch personal details from account
    if (User.Identity.IsAuthenticated == true)
    {
        var user = userManager.FindById(User.Identity.GetUserId());
        if (user != null)
        {
            //do stuff
        }
    }

    So when I retrieve a user I'd like to be able to retrieve / access the addresses that belong to that user - I have shown my data structure.

    Friday, October 7, 2016 2:46 PM
  • User283571144 posted

    Hi chilluk,

    How do I get this to hook up automatically so I can retrieve / store multiple addresses using the Identity provider code? Or am I better off putting all this in my own code and retrieving this data myself?

    As far as I know, after you use "Migration" to update the database, you could use " System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>()" method to get the current user's usermanager.

    Then you could use usermanager's FindById method t o get current user.

    At last, you could use usermanager's update method to store address into database.

    More details, you could refer to follow codes:

     public ActionResult Index()
            {
                //Store AccountAddress 
                ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
                ApplicationUserManager UserManager = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
                if (user != null)
                {
                    AccountAddress a1 = new AccountAddress();
                    a1.Address1 = "aaaa";
                    a1.Address2 = "bbbb";
                    a1.Company = "aaa";
                    a1.Country = "bbb";
                    a1.County = "aa";
                    a1.Postcode = "1111";
                    a1.Town = "aaaa";
                    a1.ID = 1;
                    a1.ApplicationUser = user;
                    ICollection<AccountAddress> userAccountAddressCollection = new Collection<AccountAddress>();
                    userAccountAddressCollection.Add(a1);
                     user.Addresses = userAccountAddressCollection;
                    UserManager.Update(user);
                }
                //read AccountAddress
                ApplicationUser user1 = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
                ApplicationUserManager UserManager1 = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
                if (user != null)
                {
                    var result = user1.Addresses;//get current user's address information
                   
                }
                
                    return View();
            }

    Best Regards,

    Brando

    Monday, October 10, 2016 12:43 PM
  • User269881539 posted

    How about retrieval of existing address records - would the FindUserByID be expected to grab these back? I already created the table and populated some data (as was moving from  a single address model) - when I retrieve my user the Addresses list is null.

    Monday, October 10, 2016 12:59 PM
  • User269881539 posted

    So I have :

    public ActionResult Address()
    {
    
        //if logged in fetch personal details from account
        if (User.Identity.IsAuthenticated == true)
        {
            var user = userManager.FindById(User.Identity.GetUserId());
            if (user != null)
            {
                Firstname = user.Firstname,
                Lastname = user.Lastname,
                Company = user.Company,
                
                var addresses = user.Addresses;
    
            }
        }        
    }

    user.Addresses is null - there is definitely data in the db table though. Is it because I am trying to hang the relationship off the AccountId field - would I have more joy with using the Id field from AspNetUsers instead?

    As I say the table already existed so I haven't done any Migration, just manually added the extra code / classes. Do I have to do something to force the identity provider to recognise this new table that I want to make part of it? My app is mostly using stored procedures for other data access in an existing db, so don't want to damage any of that.

    Wednesday, October 12, 2016 1:59 PM
  • User1104055534 posted

    Dear chilluk,

    By reviewing the above, it seems to me the issue is very probably caused as you didn't build many to many mapping between ApplicationUser and AccountAddress.

    Please kindly refer http://www.codeproject.com/Articles/234606/Creating-a-Many-To-Many-Mapping-Using-Code-First for more information.

    If you need further support on this, please visit the below link to see the various free and paid support options that are available to better meet your needs. http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone as your question falls into a category which requires a more in-depth level of support. 

    Best regards,

    Jinjie ZHOU

    Thursday, October 20, 2016 10:09 AM
  • User269881539 posted

    It wouldn't be a many to many relationship - each user can have many addresses so that's a one to many.

    Monday, October 24, 2016 10:32 AM
  • User753101303 posted

    Hi,

    One to many? But what is the purpose of  AccountAddress then? Also it seems you don't have any id in addresses? This is a context so you should be able to do with this context anything that can be done with a context. Would have to give this a try (you want one to many having just addresses for each user ?) Even if this is not what you want you could start with something simple to see if it works before proceeding further.

    Not directly related but seeing your original question I would wonder still if this is related to "identity". I would add this if yes but would add this to my regular business data context if not.

    Monday, October 24, 2016 11:29 AM