MVC No Key Defined - asp.net-mvc

I am getting this error in my MVC Application:
One or more validation errors were detected during model generation:
System.Data.Edm.EdmEntityType: : EntityType 'CustomerModel' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �Customer� is based on type �CustomerModel� that has no keys defined.
My Customer Model looks like this:
public class CustomerModel
{
public string Name { get; set; }
public int CustomerID { get; set; }
public string Address { get; set; }
}
public class CustomerContext : DbContext
{
public DbSet<CustomerModel> Customer { get; set; }
}

By default, Entity Framework assumes a key property called Id exists in your model class. Your key property is called CustomerID, so Entity Framework can't find it.
Either change the name of your key property from CustomerID to Id, or decorate the CustomerID property with the Key attribute:
public class CustomerModel
{
public string Name { get; set; }
[Key]
public int CustomerID { get; set; }
public string Address { get; set; }
}
public class CustomerContext : DbContext
{
public DbSet<CustomerModel> Customer { get; set; }
}

Related

Error while generating Model in MVC

I got the following error while generating a Model in ASP.Net:
MvcApplication1.Models.Department: : EntityType 'Department' has no key defined. Define the key for the EntityType.
department: EntityType: EntitySet 'department' is based on type 'Department' that has no keys define.
Here's a screenshot with more details:
I added the primary key to the Department table:
{
[Table("department")]
public class Department
{
public int dept id { get; set; }
public string department { get; set; }
public List<Employee> Employees { get; set; }
}
}
You need to add a [key] annotation to your deptid field:
{
[Table("department")]
public class Department
{
[Key]
public int deptid { get; set; }
public string department { get; set; }
public List<Employee> Employees { get; set; }
}
}
department table. ' {
[Table("department")]
public class Department
{
[Key]
public int dept id { get; set; }
public string department { get; set; }
public List<Employee> Employees { get; set; }
}
}`
Add key attribute, refer msdn
Another option is to change public int deptid to either public int Id or public int DepartmentId. This is a built-in EF convention, and if you use this syntax, you don't need the [Key] attribute.
https://msdn.microsoft.com/en-us/library/jj679962(v=vs.113).aspx#Anchor_1

Make foreign key to ApplicationUser in auxiliary table (EntityFramework Identity)

I have my ApplicationUser model defined like so:
public class ApplicationUser : IdentityUser
{
public virtual UserProfileInfo UserProfileInfo { get; set; }
}
With UserProfileInfo being an auxiliary table containing extra user data, defined as such:
public class UserProfileInfo
{
public int Id { get; set; }
public string SuperiorId { get; set; }
[ForeignKey("SuperiorId")]
public virtual ICollection<ApplicationUser> Superior { get; set; }
}
Each user has one or more people who are superior to them, in a business hierarchy. However, attempting to do a migration results in the following error:
The ForeignKeyAttribute on property 'Superior' on type 'Project.Models.UserProfileInfo' is not valid. The foreign key name 'SuperiorId' was not found on the dependent type 'Project.Models.ApplicationUser'.
How can I make a foreign key reference to ApplicationUser in my situation?
You need to do it the other way around, the ID is the foreignkey of the navigational property:
public class UserProfileInfo
{
public int Id { get; set; }
[ForeignKey("Superior")]
public string SuperiorId { get; set; }
public virtual ICollection<ApplicationUser> Superior { get; set; }
}

ASP.NET Identity Foreign Key as Primary key?

In my ASP.NET MVC Application, I customized the Identity to use another table for storing the users' infos such as first name, last name and birthdate. Here's the association code:
public class ApplicationUser : IdentityUser
{
public virtual UserInfo Info { get; set; }
}
public class UserInfo
{
public int Id { get; set; }
public string FirstName { get; set;
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
}
But how do you use the primary key of the ApplicationUser class as the primary key of UserInfo so that you know which user accounts the infos belong to?
The primary Key of ApplicationUser will be of a default type String, so first you will need to change your dependent class to also have a key of type string.
public class UserInfo
{
public string Id { get; set; }
public string FirstName { get; set;
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
}
Then in your IdentityDbContext implementation you will need to create a mapping to create the 1:1 relationship you are after like so.
public class MyIdentityDbContext : IdentityDbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ApplicationUser>().HasRequired(x => x.Info).WithRequiredDependent();
base.OnModelCreating(modelBuilder);
}
}
and that should work

EntityType 'SelectListItem' has no key defined. Define the key for this EntityType

i Have a Model Class
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public ICollection<SelectListItem> CourseList { get; set; }
}
and the
public class StudentContext : DbContext
{
public DbSet<Student> Students { get; set; }
}
and i try ti use it as
List<Student> sList = db.Students.ToList();
and i am getting following error
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'SelectListItem' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'SelectListItems' is based on type 'SelectListItem' that has no keys defined.
Please suggest where i am doing wrong.
Add [NotMapped] annotation to the LIST class
[NotMapped]
public List<SelectListItem> ListItems { get; set; }
NotMapped Code first convention dictates that every property that is of a supported data type is represented in the database. But this isn’t always the case in your applications. For example you might have a property in the Blog class that creates a code based on the Title and BloggerName fields. That property can be created dynamically and does not need to be stored. You can mark any properties that do not map to the database with the NotMapped annotation such as this BlogCode property.
[NotMapped]
public string BlogCode
{
get
{
return Title.Substring(0, 1) + ":" + BloggerName.Substring(0, 1);
}
}
You can refer to the link here on EF code first Data Annotations
You should not be attempting to store SelectListItem in the database, as this is MVC specific concept. Instead create a custom entity class and use it instead;
public class Course
{
public int CourseId { get; set; }
public string CourseTitle { get; set; }
}
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public ICollection<Course> CourseList { get; set; }
}
public class StudentContext : DbContext
{
public DbSet<Student> Students { get; set; }
public DbSet<Course> Courses { get; set; }
}

EntityType has no key defined. [Key] is presented

Entity
public class Region
{
[Key]
public int ID;
public string Name;
public string Description;
}
Model
public class RegionModel
{ [Key]
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
Errors
System.Data.Edm.EdmEntityType: : EntityType 'Region' has no key
defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �Regions� is based
on type �Region� that has no keys defined.
Your class fields need to be changed to properties for EF to use the class correctly;
public class Region
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class Region
{
[Key]
public int RegionId{get;set;}
public string Name{get;set;}
public string Description{get;set;}
}
public class RegionModel
{ [Key]
public int RegionModelId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
It works if you have it like ClassNameId.You can even remove [Key] attribute now.
Maybe a strange answer to your problem. But be sure that your project compiles first. i got the same errors when i had added the dataanotations without compiling the project.
I think the code is generated with some kind of reflection.

Resources