title: ASP.net core3.1 mvc data display
body: <p>I am new in APS.Net core MVC and want to display database data on a web page. I have created a Model class similar to the database table as shown in the code below:</p>
  
  <pre><code> public class Customer
  {   [Key]
      public int Id { get; set; }
      public string Name { get; set; }
      public int Age { get; set; }
      public DateTime DateOfBirth{ get; set; }
      public string Address{ get; set; }
   }
  </code></pre>
  
  <p>and a Controller class :</p>
  
  <pre><code> public class CustomersController : Controller
  {
      private readonly ApplicationDbContext _db;
      public CustomersController(ApplicationDbContext db)
      {
          _db = db;
      }
      // Get Data 
      public  async Task&lt;IActionResult&gt; Index()
      {
          return View(await _db.Customers.ToListAsync());
          }
      }
  </code></pre>
  
  <p>and a view page</p>
  
  <pre><code> @model IEnumerable&lt;Customers&gt;
  
  @{
  ViewData["Title"] = "Customers List";
  Layout = "~/Views/Shared/_Layout.cshtml";
  }
  
  &lt;h1&gt;Records&lt;/h1&gt;
  
  &lt;p&gt;
      &lt;a asp-action="Create"&gt;Create New&lt;/a&gt;
  &lt;/p&gt;
  &lt;table class="table"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.Id)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.Name)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.Age)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.DateOfBirth)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.Address)
              &lt;/th&gt;  
       &lt;/tr&gt;
      &lt;/thead&gt;
  </code></pre>
  
  <p>When I run the application and navigate to the page  the following message appears:</p>
  
  <blockquote>
    <p><strong>A database operation failed while processing the request.
    InvalidCastException: Unable to cast object of type 'System.Int64' to
    type 'System.Int32'.</strong></p>
    
    <p>Applying existing migrations for ApplicationDbContext may resolve this
    issue There are migrations for ApplicationDbContext that have not been
    applied to the database</p>
    
    <p>20200305172409_Customers Apply Migrations</p>
    
    <p>In Visual Studio, you can use the Package Manager Console to apply
    pending migrations to the database:</p>
    
    <p>PM> Update-Database Alternatively, you can apply pending migrations
    from a command prompt at your project directory:</p>
    
    <blockquote>
      <p>dotnet ef database update</p>
    </blockquote>
  </blockquote>
  
  <p>I tried to add-migration and update-database but the following message appears in the Package Manager Console:</p>
  
  <blockquote>
    <p>Failed executing DbCommand (224ms) [Parameters=[], CommandType='Text',
    CommandTimeout='30'] CREATE TABLE [Customers] ( There is already an
    object named 'Customers' in the database.</p>
  </blockquote>
  
  <p>The connection service in startup.cs is as default :</p>
  
  <pre><code>services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
                  options.UseSqlServer(
                      Configuration.GetConnectionString("DefaultConnection")));
  </code></pre>