Mikkel
Mikkel

Reputation: 1811

Deployed Web api with Entity Framework to Azure, worked 1 hour, now getting error 500

I have almost just deployed my Web Api project to a free Azure server with a basic DB on it. After I published it to the site, it worked quite well. I could call it from my angular 2 application and receive the data from the database.

2 hours after when I try to use it again, I am now receiving an error 500 when calling the valid REST method.

When I debug my angular 2 application, I can see the following in the Network tab in chrome.

xhr_backend.ts:82 GET http://sitename.azurewebsites.net/api/getbookings 500 (Internal Server Error) 

Have also tried with postman, it also gets that error and the following I get back are:

{
  "Message": "An error has occurred."
}

Have also tried to stop the site and start it again, did not help.

It must be something with Entity framework, because the site is running.

I'm using Migrations, and Have set up some test data in the configuration file, like this:

var pet = new List<Pet>
        {
            new Pet {BookingId = 1, PetType = PetType.Dog},
            new Pet {BookingId = 2, PetType = PetType.Dog},
            new Pet {BookingId = 2, PetType = PetType.Cat},
            new Pet {BookingId = 3, PetType = PetType.Dog},
            new Pet {BookingId = 4, PetType = PetType.Dog},
            new Pet {BookingId = 4, PetType = PetType.Cat},
            new Pet {BookingId = 5, PetType = PetType.None},
            new Pet {BookingId = 6, PetType = PetType.Cat},
            new Pet {BookingId = 6, PetType = PetType.Dog},
            new Pet {BookingId = 7, PetType = PetType.None},
            new Pet {BookingId = 8, PetType = PetType.None},
        };

        pet.ForEach(p => context.Pets.AddOrUpdate(s => s.Id, p));
        context.SaveChanges();

I'm not sure how to find what could be wrong here. First time I have deployed an application to Azure :D

Do any know if Azure are calling update-database more than once? (when you deploy the application)

When I try to enter update-database on the local databae, I get the error:

Sequence contains more than one element

Maybe it could be related to that? Hope someone have some tricks I can try.

Update:

After some investigation, I turned on error for the api. I now get the following error:

<ExceptionMessage>Sequence contains more than one element</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>

Upvotes: 1

Views: 130

Answers (1)

Nkosi
Nkosi

Reputation: 247088

This exception was caused when trying to use AddOrUpdate with specifying identifier expression, where there might be more than one match for the expression.

Internally that function will use SingleOrDefault and if there is found more than one match element, exception will be thrown.

Upvotes: 1

Related Questions