How solve this problem in MVC3

Posted by Cpatil1000 under ASP.NET MVC on 9/10/2014 | Points: 10 | Views : 615 | Status : [Member] | Replies : 1
Hi,
I am new in MVC. I have got problem. I am tiring bind a database to webgrid in MVC3.
I am getting following error. I have not created Entity. But I want programmatically how bind a database to webgrid. On coding line
users = dc.District.ToList();

Error:
System.Data.Edm.EdmEntityType: : EntityType 'District' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet ?District? is based on type ?District? that has no keys defined.
Controller Code :
public ViewResult List()
{
var users = new List<District>();
//here MyDatabaseEntities is the dbcontext
using (DistrictDBContext dc = new DistrictDBContext())
{
users = dc.District.ToList();
}
return View(users);
}

Model :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Data.Entity;
using System.Configuration;

namespace MVCTest.Models
{
public class District
{
public string DISTRICT_ID { get; set; }
public string DISTRICT_NAME { get; set; }
public string YEAR { get; set; }
}

public class DistrictDBContext : DbContext
{
public DistrictDBContext()
: base("name=SqlConnectionString")
{
}
public DbSet<District> District { get; set; }

}


}

List.cshtml
@model List<MVCTest.Models.District>

@{
ViewBag.Title = "List of users";
var grid = new WebGrid(source: Model, canPage: true, rowsPerPage: 10);
grid.Pager(WebGridPagerModes.All);
}
<h2>
List of Users</h2>
<style type="text/css">
/*Here we will add css for style webgrid*/
.webgrid-table
{
font-family: "Trebuchet MS" , Arial, Helvetica, sans-serif;
font-size: 1.2em;
width: 100%;
display: table;
border-collapse: separate;
border: solid 1px #98BF21;
background-color: white;
}
.webgrid-table td, th
{
border: 1px solid #98BF21;
padding: 3px 7px 2px;
}
.webgrid-header
{
background-color: #A7C942;
color: #FFFFFF;
padding-bottom: 4px;
padding-top: 5px;
text-align: left;
}
.webgrid-footer
{
}
.webgrid-row-style
{
padding: 3px 7px 2px;
}
.webgrid-alternating-row
{
background-color: #EAF2D3;
padding: 3px 7px 2px;
}
</style>
<div id="content">
@grid.GetHtml(
tableStyle: "webgrid-table",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
rowStyle: "webgrid-row-style",
columns: grid.Columns(
//here i will add column for serial no
grid.Column(header: "Serial No", format: @<text><div>@(item.WebGrid.Rows.IndexOf(item) + 1)</div></text>),
grid.Column(columnName: "DISTRICT_ID", header: "DISTRICT ID"),
grid.Column(columnName: "DISTRICT_NAME", header: "DISTRICT NAME"),
grid.Column(columnName: "YEAR", header: "YEAR")

))
</div>




Responses

Posted by: Bandi on: 9/10/2014 [Member] [MVP] Platinum | Points: 25

Up
0
Down
namespace MVCTest.Models 
{
public class District
{
[Key] // put this [Key] property for the column which has Primary Key / Unique Key
public string DISTRICT_ID { get; set; }
public string DISTRICT_NAME { get; set; }
public string YEAR { get; set; }
}


EF can work only when it knows primary key of table. By default EF recognize as primary key property with name Id. If your table has another primary key, you can mark it with attribute [Key] or set Key with fluent configuration.

Refer
http://stackoverflow.com/questions/13862013/entitytype-category-has-no-key-defined-define-the-key-for-this-entitytype

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Cpatil1000, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response