Child class of EF Entity not being committed on the DBContext because it is not part of the model

59 Views Asked by At

I am using EF6. I created an entity in the EF model which acts as a super class called AppUser containing all the properties I want to generate in the database. However, I created another class called AppUserM inheriting from AppUser; the child class has several methods for some UI operations. See the AppUser class below:

[Table("appUser")]
public class AppUser
{
    public AppUser()
    {
        this._AccessRights = new HashSet<AccessRight>();
        this._ActivityLog = new HashSet<ActivityLog>();
    }
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int userId { get; set; }
    [MaxLength(50)]
    [DisplayName("Username")]
    public string username { get; set; }
    [DataType(DataType.Password)]
    [MaxLength(2000)]
    [DisplayName("Password")]
    public string password { get; set; }
    [MaxLength(4000)]
    [DisplayName("FullName")]
    public string fullname { get; set; }
    public bool isADUser { get; set; }
    [ForeignKey("BankBranch")]
    public int branchId { get; set; }
    [ForeignKey("AppRole")]
    public int roleId { get; set; }
    public virtual BankBranch BankBranch { get; set; }
    public virtual AppRole AppRole { get; set; }
    public virtual ICollection<AccessRight> _AccessRights { get; set; }
    public virtual ICollection<ActivityLog> _ActivityLog { get; set; }

}

the child class is shown below:

public class AppUserM : AppUser
{
    public void Add()
    {
        using (var cntx = new JEntityDbContext())
        {
            cntx.AppUsers.Add(this);
            cntx.SaveChanges();
        }
    }
    public void Read()
    { 

    }
}

Executing this raises an exception at the Add() method of the AppUserM exactly at cntx.AppUsers.Add(this) see the implementation below;

     AppUserM user = new AppUserM
     {
          username = "Hakeem",
          password = "pass",
          roleId = 1,
          isADUser = false,
          fullname = "Ojulari Hakeem Olusegun"
     };
     user.Add();

The exception is :

Additional information: The entity type AppUserM is not part of the model for the current context.

I don't want to map the classes in the Add() method like this

AppUser user = new AppUser {username = this.username};

then this after

cntx.AppUsers.Add(user);

I want to minimize typing time and ensure efficiency. Is there any better way to achieve this. Thanks for your contributions

1

There are 1 best solutions below

6
On BEST ANSWER

I don't expect that EF can handle this, since EF creates proxies for your entity classes (to handle lazy loading and such by overriding your virtual navigation props).

I would just add the methods to the existing AppUser class.

[Table("appUser")]
public class AppUser
{
    public AppUser()
    {
        this._AccessRights = new HashSet<AccessRight>();
        this._ActivityLog = new HashSet<ActivityLog>();
    }
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int userId { get; set; }
    // ... other properties

    public void Add()
    {
        using (var cntx = new JEntityDbContext())
        {
            cntx.AppUsers.Add(this);
            cntx.SaveChanges();
        }
    }
    public void Read()
    { 

    }
}