0

我提到了许多堆栈溢出问题,例如EntityType 'MyProfile' has no key defined。定义此 EntityType 的键。提到的解决方案是定义[Key]属性。

即使添加了 [Key] 属性(当我尝试插入员工时),我也会收到以下错误。我们如何解决这个问题?

EntityType 'Role' 没有定义键。定义此 EntityType 的键。

注意:即使在为 RoleID 添加设置器后,我也会收到相同的错误。

public abstract int RoleID { get; set; }

注意:角色类是abstract

EF 代码优先

  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();
        }
    }

实体

  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<Role> roles;

    public virtual IList<Role> RolesList
    {
        get
        {
            return roles;
        }

    }

    public bool IsActiveEmployee
    {
        get
        {
            return isActiveEmployee;
        }
        set
        {
            isActiveEmployee = value;
        }
    }

    public int EmployeeID { get; set; }

    //Constructor
    public Employee()
    {
        roles = new List<Role>();
    }

    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;
            }
        }
    }

}
4

1 回答 1

1

除非我对此更加环保,否则我认为,您应该阅读使用实体框架的 Code First 方式的工作原理。:)

以下是我将如何创建类似于您正在尝试做的事情,我实际上还没有构建代码,因此可能会出现错误:

public class Role
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)] // since you set the IDs in code
    public int RoleID { get; set; }
    public string RoleName { get; set; }
}

public class Employee
{
    public int EmployeeID { get; set; }
    public ICollection<Role> Roles { get; set; } // make it virtual for lazy loading
}

在 My19June_A 中:

public class My19June_A : DbContext
{
    public DbSet<Employee> { get; set; }
    public DbSet<Role> { get; set; }

    static My19June_A()
    {
        Database.SetInitializer<RegistryContext>(new CreateInitializer());
    }

    class CreateInitializer : CreateDatabaseIfNotExists<RegistryContext>
    {
        protected override void Seed(RegistryContext context)
        {
            void Seed()
            {
                var programmerRole = new Role() { RoleID = 101, RoleName = "Programmer" };
                var managerRole = new Role() { RoleID = 102, RoleName = "Manager" };

                context.Roles.Add(programmerRole);
                context.Roles.Add(managerRole);
                context.SaveChanges();        
            }
        }
    }
}

您可以通过选择角色各自的 ID 来获取角色。然而,另一种可能性是完全跳过将角色存储在数据库中,因为您似乎需要针对不同角色的特定类。然后可以将数据库中的角色存储为一个 int 值。

于 2013-06-19T16:19:17.510 回答