2

Problem: For a school project I created a REST web service which is linked with a database that gets his data from an excel file. In my Migration/Configuration.cs class I read the excel file and put them in the database. But when I try to publish my project on Azure, it seems that he can't find the excel file and I have absolutely no idea what the path of that excel file should be. (It does work when I change the seed method of Configuration.cs to some hardcoded stuff). I already tried to change the path to Server.MapPath or HttpRuntime.AppDomainAppPath, but it doesn't work... Does anyone have an idea how to solve this problem? Thanks a lot!!!

Project structure: https://i.stack.imgur.com/x3sg3.jpg

Configuration.cs:

namespace RestServiceTest.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.IO;
    using System.Linq;
    using System.Web;
    using WineFoodModel;
    internal sealed class Configuration : DbMigrationsConfiguration<RestServiceTest.Models.WineRestTestContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(RestServiceTest.Models.WineRestTestContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
            string path = "C:\\Users\\Chen\\Documents\\School\\Eindwerk\\afstudeerproject\\RestServiceTest\\RestServiceTest\files\\Virtuele Sommelier juli 2015.xlsx";
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@path);
            Microsoft.Office.Interop.Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Microsoft.Office.Interop.Excel.Range xlRange = xlWorksheet.UsedRange;

            int rCount = xlRange.Rows.Count;
            int cCount = xlRange.Columns.Count;

            string stringRange = "A1:L" + rCount;
            object[,] objectArray = xlWorksheet.get_Range(stringRange).Value2;

            for (int i = 1; i <= rCount; i++)
            {
                SubCategory subCategory = new SubCategory((string)objectArray[i, 2], new Category((string)objectArray[i, 1]));
                Food food = new Food((string)objectArray[i, 3], subCategory);

                WineFoodModel.Type type = (WineFoodModel.Type)Enum.Parse(typeof(WineFoodModel.Type), (string)objectArray[i, 4], true);
                string naamWijn = (string)objectArray[i, 5];
                string url = (string)objectArray[i, 6];
                double price = (double)objectArray[i, 7];
                Region region = new Region((string)objectArray[i, 9], new Country((string)objectArray[i, 8]));
                string appelatie = (string)objectArray[i, 10];
                bool bio = checkBio((string)objectArray[i, 11]);
                string description = (string)objectArray[i, 12];

                //Wine wine = new Wine(type, naamWijn, url, price, description, region, appelatie, bio);
                var wine = new Wine
                {
                    Appelatie = appelatie,
                    Bio = bio,
                    Description = description,
                    Name = naamWijn,
                    PictureHtml = url,
                    Price = price,
                    Region = new Region
                    {
                        Name = objectArray[i, 9] as string,
                        Country = new Country
                        {
                            Name = objectArray[i, 8] as string
                        }
                    },
                    WineType = type
                };
                context.Wines.AddOrUpdate(p => p.Name,
                    wine);
                //Salade    Zomerse salade  Zomerse salade  Wit 3P Picpoul de Pinet http://u.jimdo.com/www70/o/s144a62240ee1d941/img/i2876998c8f7cd492/1413374802/orig/image.jpg     8.50 €     Frankrijk   Languedoc   Picpoul de Pinet        De druif Picpoul de Pinet geeft  pittig en verfrissend wijnen met veel citrus, gele pruimen, abrikozen, bloesems en een goed gedefinieerde mineraliteit.  Perfecte zomerse verfrisser en dé zuid-Franse wijn voor bij uw Vis - Schaaldieren en mosselen gerechten.                                                                  
            }
        }
        public static bool checkBio(string text)
        {
            if (text != null && text.Equals("Y"))
            {
                return true;
            }
            return false;
        }
    }
}
199346chen
  • 67
  • 9
  • Can you post your attempts using Server.MapPath? Obviously, the hard-coded path won't exist in Azure, right? – Ashraf Iqbal Feb 16 '16 at 22:32
  • Read this article and you should be able to figure out: http://stackoverflow.com/questions/16260975/entity-framework-how-to-get-relative-file-path-in-seed-method – Ashraf Iqbal Feb 16 '16 at 22:36

1 Answers1

2

do you include the excel file as part of your project? (after build, check your bin folder). if not, please include your excel file.

when deploy to Azure (Azure App Service i assume), your file`s absolute path is

Environment.ExpandEnvironmentVariables(@"%HOME%\site\wwwroot\{relative file path}")
Xiaomin Wu
  • 3,621
  • 1
  • 15
  • 14
  • I can't find my excel file in the bin folder after build, although I added it as an existing item to my project... – 199346chen Feb 17 '16 at 09:21
  • Wouldn't you want the path to work locally as well as on App Service? This doesn't seem right unless you force IIS locally.... – Jake Smith Jan 18 '21 at 23:32