2

Hi been trying to seed my database with a custom file it works localy. the UserSeedData.Json is located in my API/Data/ folder. where i have the script to seed. this is what the script look like

namespace API.Data
{
    public class Seed
    {
        public static async Task SeedUsers(UserManager<AppUser> userManager, 
            RoleManager<AppRole> roleManager)
        {
            if (await userManager.Users.AnyAsync()) return;

            var userData = await System.IO.File.ReadAllTextAsync("Data/UserSeedData.json");
            var users = JsonSerializer.Deserialize<List<AppUser>>(userData);
            if (users == null) return;

            var roles = new List<AppRole>
            {
                new AppRole{Name = "Member"},
                new AppRole{Name = "Admin"},
                new AppRole{Name = "Moderator"},
            };

            foreach (var role in roles)
            {
                await roleManager.CreateAsync(role);
            }
            
            foreach (var user in users)
            {
                user.UserName = user.UserName.ToLower();
                await userManager.CreateAsync(user, "Password");
                await userManager.AddToRoleAsync(user, "Member");
            }

            var admin = new AppUser
            {
                UserName = "admin"
            };

            await userManager.CreateAsync(admin, "Password");
            await userManager.AddToRolesAsync(admin, new[] {"Admin", "Moderator"});
        }
    }
}

the error i get when i start the app on heroku

2021-02-23T14:11:07.621267+00:00 app[web.1]:       an error occured during migration.
2021-02-23T14:11:07.621268+00:00 app[web.1]:       System.IO.DirectoryNotFoundException: Could not find a part of the path '/app/heroku_output/Data/UserSeedData.json'.
2021-02-23T14:11:07.621276+00:00 app[web.1]:          at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)
2021-02-23T14:11:07.621288+00:00 app[web.1]:          at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode)
2021-02-23T14:11:07.621289+00:00 app[web.1]:          at System.IO.FileStream.OpenHandle(FileMode mode, FileShare share, FileOptions options)
2021-02-23T14:11:07.621289+00:00 app[web.1]:          at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
2021-02-23T14:11:07.621289+00:00 app[web.1]:          at System.IO.File.AsyncStreamReader(String path, Encoding encoding)
2021-02-23T14:11:07.621290+00:00 app[web.1]:          at System.IO.File.InternalReadAllTextAsync(String path, Encoding encoding, CancellationToken cancellationToken)
2021-02-23T14:11:07.621290+00:00 app[web.1]:          at API.Data.Seed.SeedUsers(UserManager`1 userManager, RoleManager`1 roleManager) in /tmp/build_a486cf24/API/Data/Seed.cs:line 17
2021-02-23T14:11:07.621290+00:00 app[web.1]:          at API.Program.Main(String[] args) in /tmp/build_a486cf24/API/Program.cs:line 33

what can i do to make the path to the file be correct by heroku? System.IO.DirectoryNotFoundException: Could not find a part of the path '/app/heroku_output/Data/UserSeedData.json' what do i do to fix this?

RangerRanger
  • 2,455
  • 2
  • 16
  • 36

2 Answers2

1

You can copy the UserSeedData.Json file to app/heroku_output/Data/ path by adding the below configuration in your project.csproj file. This configuration will copy the file to configured path when the Publish completes.

<Target Name="CopyCustomContentOnPublish" AfterTargets="Publish">
   <Copy SourceFiles="Data/UserSeedData.json" DestinationFolder="$(PublishDir)/Data" /> 
</Target>

Reference credit post: https://stackoverflow.com/a/44378406/8680456

Prasad Kaiche
  • 591
  • 3
  • 10
0

So I come up with a solution its probly not a "corret" one but it works. i moved my UserSeedData.Json to my wwwroot/assets folder and changed the path to

 var userData = await System.IO.File.ReadAllTextAsync("wwwroot/assets/UserSeedData.Json");

it works! but i don't know if there is a folder i should create that hold assets that heroku build can read that will not become invalid pathed.