I have recently implemented an MVC3 website and had to now include data from a view.
As Shown below I went about setting it up much like I would a table then I got the below error.
I am unsure of how to set this up .
I have provided some of the classes below to help point me in the right direction.
One or more validation errors were detected during model generation:
System.Data.Edm.EdmEntityType: : EntityType ‘AJF_ProfitCentres’ has no key defined.
Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType:
EntitySet �ProfitCentres� is based on type
�AJF_ProfitCentres� that has no keys
public class NavContext : DbContext
{
public DbSet<Account> Accounts { get; set; }
public DbSet<AJF_ProfitCentres> ProfitCentres { get; set; }
public NavContext()
: base("NavContext")
{
}
}
public class NavRepository : INavRepository
{
private readonly NavContext _context;
public NavRepository(NavContext context)
{
_context = context;
}
public List<AJF_ProfitCentres> GetAllProfitCentres()
{
return _context.ProfitCentres.ToList();
}
}
public class AJF_ProfitCentres
{
public int ProfitCentreId;
[MaxLength(30, ErrorMessage = "ProfitCentre cannot be longer than 30 characters.")]
public string ProfitCentre { get; set; }
[MaxLength(64, ErrorMessage = "AccountId cannot be longer than 64 characters.")]
public string AccountId { get; set; }
}
As a followup I ended up creating it as a seperate datacontext built from the view rather than code first works like a dream.