I have this class example:
public class User : APerson
{
private string _userName;
[DataMember]
public virtual string UserName
{
get { return _userName; }
set
{
if (string.IsNullOrWhiteSpace(value))
{
throw new FormatException(ErrorMessage.User.USERNAME_REQUIRED);
}
if (value.Length > 50)
{
throw new FormatException(ErrorMessage.User.USERNAME_TOO_LONG);
}
_userName = value;
}
}
[DataMember]
public virtual string Password { get; set; }
[DataMember]
public virtual bool IsActiveDirectory { get; set; } = false;
[IgnoreDataMember]
public virtual IList<Application> Applications { get; set; }
[IgnoreDataMember]
public virtual IList<UserAccessKey> UserAccessKeys { get; set; }
[IgnoreDataMember]
public virtual IList<UserApplication> UserApplications { get; set; }
[IgnoreDataMember]
public virtual AClient Client { get; set; }
[IgnoreDataMember]
public virtual IList<UserLog> UserLogs { get; set; }
}
The AClient, is this abstract class:
public abstract class AClient
{
private string _companyName;
private string _company;
[DataMember]
public virtual int Id { get; set; }
[DataMember]
public virtual Guid Hash { get; set; }
[DataMember]
public virtual bool IsManager { get; set; }
[DataMember]
public virtual string CompanyName
{
get { return _companyName; }
set
{
if (!string.IsNullOrEmpty(value) && value.Length > 100)
{
throw new Exception(ErrorMessage.Client.COMPANY_NAME_TOO_LOG);
}
_companyName = value;
}
}
[DataMember]
public virtual string Company
{
get { return _company; }
set
{
if (!string.IsNullOrEmpty(value) && value.Length > 150)
{
throw new Exception(ErrorMessage.Client.COMPANY_TOO_LONG);
}
_company = value;
}
}
[DataMember]
public virtual bool IsActive { get; set; }
[DataMember]
public virtual string CssFileExtensionName { get; set; }
}
So far so good. The problem is that if I were to see LINQ try to find in the Client attribute of the User class, the Description attribute, which does not exist in the abstract class, I will have a compilation error.
What to do in these cases? How to explain to LINQ which concrete class inherits from the abstract class, which has this attribute.
In addition: If I have the following example:
public virtual IList<APerson> Patients { get; set; } = new List<Patient>();
I have a compile error, even the Patient class inheriting from APerson.
If I do this below, the second line works. The first one is not!
public virtual IList<APerson> Patients { get; set; } = new List<Patient>();
public APerson person { get; set; } = new Patient();
I can not understand these problems. Can anyone give me a light? Note: Starting now with ID