0

I am using ASP.NET MVC5 Identity. I want to create a user and at the same time user can upload profile picture.

In Identity Model i add this property

  public string ImagePath { set; get; }

So I create user using this code

 var adminresult = await UserManager.CreateAsync(user,cvm.Password);

So now i want to save the image in directory using user id folder .like this code

string fileName = Path.GetFileName(imagePath.FileName);
                            string fullDirectoryPath = "~/Content/Images/Users/" + user.Id + "/Thumb";
                            if (!Directory.Exists(Server.MapPath(fullDirectoryPath)))
                            {
                                Directory.CreateDirectory(Server.MapPath(fullDirectoryPath));
                            }
                            var path = Path.Combine(Server.MapPath(fullDirectoryPath), fileName);
                            imagePath.SaveAs(path);
                            var photopath = "~/Content/Images/Users/" + user.Id + "/Thumb/" + fileName;
                            user.ImagePath = photopath;

but problem is now i need to save this directory in database . So i need to update the user table again .

My Question is,How can i save database at one time. I don't want to run create and again update query for image column.

Nazmul Hossain
  • 2,085
  • 5
  • 25
  • 34
  • Why would you want to save the file with the user ID? Just save it with a `Guid` so its unique and then you can save all in one database call –  Aug 04 '16 at 02:13
  • I am also thinking about Guid .But after reading this i am not sure which way is should go http://stackoverflow.com/questions/39771/is-a-guid-unique-100-of-the-time – Nazmul Hossain Aug 04 '16 at 05:40
  • Just override `UserManager.CreateAsync` and add your photo logic there before calling `base.CreateAsync(...)`. But the real question is - how much performance impact there is when you do 2 small DB transactions instead of one. Do you have millions of users and thousands more created every hour? – trailmax Aug 04 '16 at 15:55
  • @trailmax Thanks for reply. I will do that. Not a big application.Just want to know best practices. – Nazmul Hossain Aug 05 '16 at 07:50

0 Answers0