Note that there are some explanatory texts on larger screens.

plurals
  1. PO“EntityType has no key defined” Error even after adding [Key]
    primarykey
    data
    text
    <p>I have referred many stack overflow questions like <a href="https://stackoverflow.com/questions/5306947/entitytype-myprofile-has-no-key-defined-define-the-key-for-this-entitytype">EntityType &#39;MyProfile&#39; has no key defined. Define the key for this EntityType</a>. And the solution mentioned is to define <code>[Key]</code> Attribute.</p> <p>I am getting the following error even after adding the [Key] attribute (when I try to insert an employee). How can we resolve this?</p> <blockquote> <p>EntityType 'Role' has no key defined. Define the key for this EntityType.</p> </blockquote> <p>Note: I am getting the same error even after adding a setter for RoleID. </p> <pre><code>public abstract int RoleID { get; set; } </code></pre> <p>Note: The Role class is <code>abstract</code> class</p> <p><strong>EF Code First</strong></p> <pre><code> public static void InsertEmployees() { string connectionstring = @"Data Source=.;Initial Catalog=My19June_A;Integrated Security=True;Connect Timeout=30"; using (var db = new My19June_A(connectionstring)) { Employee emp1= new Employee(); emp1.EmployeeID = 1; emp1.IsActiveEmployee = true; Employee emp2 = new Employee(); emp2.EmployeeID = 2; emp2.IsActiveEmployee = true; db.Employees.Add(emp1); db.Employees.Add(emp2); int recordsAffected = db.SaveChanges(); } } </code></pre> <p><strong>Entities</strong></p> <pre><code> public abstract class Role : IEntityWithKey { public EntityKey EntityKey { get; set; } public abstract string RoleName { get; } [Key] public abstract int RoleID { get; } } public class ProgrammerRole : Role, IEntityWithKey { public EntityKey EntityKey { get; set; } public override string RoleName { get { return "Programmer"; } } [Key] public override int RoleID { get { return 101; } } } public class ManagerRole : Role, IEntityWithKey { public EntityKey EntityKey { get; set; } public override string RoleName { get { return "Manager"; } } [Key] public override int RoleID { get { return 102; } } } public class Employee : IEntityWithKey { public EntityKey EntityKey { get; set; } private bool isActiveEmployee; private IList&lt;Role&gt; roles; public virtual IList&lt;Role&gt; RolesList { get { return roles; } } public bool IsActiveEmployee { get { return isActiveEmployee; } set { isActiveEmployee = value; } } public int EmployeeID { get; set; } //Constructor public Employee() { roles = new List&lt;Role&gt;(); } public void TerminateEmployeeByRole(Role role) { if (RolesList == null) { //If employee has no role, make as inactive isActiveEmployee = false; } else { //If employee has no role other than the input role, make as inactive RolesList.Remove(role); if (RolesList.Count == 0) { isActiveEmployee = false; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload