How to connect database to web application ASP.NET MVC in Visual Studio 2017?

Working with Entity Framework. A database placed in the App_Data folder - Database1.mdf. Create a model, context, controller.
namespace Test.Models
{
 public class Team
{
 public int Id { get; set; }
 public string Name { get; set; }
 public string Coach { get; set; }
}
 public class Player
{
 public int Id { get; set; }
 public string Name { get; set; }
 public int Age { get; set; }
 public string Position { get; set; }

 public int? TeamId { get; set; }
 public Team Team { get; set; }
}
public class Context : DbContext
{
 public DbSet<Team> Teams { get; set; }
 public DbSet<Player> Players { get; set; }
}

namespace Test.Controllers
{
 public class HomeController : Controller
{
 Context c = new Context();
 public ActionResult Index()
{
 var players = c.Players.Include(p => p.Team);
 return View(players.ToList());
}
}
}

The view looks the same way as in the example below. In the Web.prescribe config connection string as follows
<connectionStrings>
 <add name="Context" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename='|DataDirectory|\Database1.mdf';Integrated Security=True" providerName="System.Data.SqlClient"/>
 </connectionStrings>

When you run the application, VS gives the following error, highlighting the line with the data output by the controller:
System.Data.SqlClient.SqlException: "Cannot create file 'c:\users\alex\source\repos\Test\Test\App_Data\Database1.mdf' because it already exists. Change the file path or the file name, and retry the operation.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors."

Renaming or creating another database does not help. What could be the error?

There is also a database on MS SQL Server 2012. Try to connect it using the model ADO.NET EDM. VS creates the necessary models, but as they continue to work is unclear. The creation of a class context as the first case does not help - the data from DB do not load on the page.
namespace MusicShopWeb
{
 using System;
 using System.Collections.Generic;
 using System.Data.Entity;

 public partial class Buyers
{
 public int Id { get; set; }
 public string fio { get; set; }
 public string address { get; set; }
 public string phone { get; set; }
}
 public class BuyersContext : DbContext
{
 public DbSet<Buyers> Buyers { get; set; }
}
}
namespace MusicShopWeb.Controllers
{
 public class Buyersontroller : Controller
{
 private BuyersContext db = new BuyersContext();

 public ActionResult Index()
{
 return View(db.Buyers.ToList());
}
}

@model IEnumerable<MusicShopWeb.Buyers>

@{
 Layout = null;
}

<!DOCTYPE html>

<html>
the <head>
 <meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
the <body>
 <table class="table">
the <tr>
<th>
 @Html.DisplayNameFor(model => model.fio)
</th>
<th>
 @Html.DisplayNameFor(model => model.address)
</th>
<th>
 @Html.DisplayNameFor(model => model.phone)
</th>
<th></th>
</tr>
 @foreach (var item in Model) {
the <tr>
<td>
 @Html.DisplayFor(modelItem => item.fio)
</td>
<td>
 @Html.DisplayFor(modelItem => item.address)
</td>
<td>
 @Html.DisplayFor(modelItem => item.phone)
</td>
</tr>
}
</table>
</body>
</html>

How to display data from database?
March 23rd 20 at 19:31
1 answer
March 23rd 20 at 19:33
How trying to work with EF? Judging by the code this Code First approach. Try to remove a database from App_Data. Because the error indicates that the database already exists and cannot be created.

Find more questions by tags SQL ServerVisual StudioASP.NET