API Controller

[HttpGet] public HttpResponseMessage Get() { using (LeaderBordContext db = new LeaderBordContext()) { List<Game> games = db.Games.OrderBy(a => a.Name).ToList(); HttpResponseMessage response; response = Request.CreateResponse(HttpStatusCode.OK, games); return response; } } 

Context

  public class LeaderBordContext : DbContext { public DbSet<Gamer> Gamers { get; set; } public DbSet<Game> Games { get; set; } public LeaderBordContext() : base("LeaderBordContext") {} } 

The server returns an error

"An error has occurred.", "ExceptionMessage": "Cannot create file 'C: \ Users \ PC \ Documents \ Visual Studio 2013 \ Projects \ LeaderBord \ LeaderBord \ App_Data \ lbdb.mdf' because it already exists. Change the file I’m not able to create a file name or a file name.

The string to connect to the database in the Web.config file indicated. What could be?

  • Show web.config, or rather connectionStrings. - Murad
  • Here’s <connectionStrings> <add name = "LeaderBordContext" connectionString = "Data Source = (LocalDB) \ v11.0; AttachDbFilename = '| DataDirectory | \ lbdb.mdf'; Integrated Security = True" providerName = "System.Data.SqlClient "/> </ connectionStrings> - Anton Evseev
  • You may need to disable initialization by default. Add to the constructor. System.Data.Entity.Database.SetInitializer <LeaderBordContext> (null); - Murad
  • My initializer is on. I initialize the database with such an initializer DropCreateDatabaseIfModelChanges (I have tried others). I call the Seed function. Everything works fine and connects to the database if through the MVC approach. But through the API, that's the trouble. Your code did not match. Incorrect syntax - Anton Evseev
  • Go to "C: \ Users \ PC \ Documents \ Visual Studio 2013 \ Projects \ LeaderBord \ LeaderBord \ App_Data \" and delete the lbdb.mdf file and try to start the project again. - Elvin No Matter

0