Search code examples
c#.netwcfentity-frameworkodata

Found wrong Inheritance and Interface on Partial Classes


I am using the inheritance and Interfaces concepts in my partial classes and my 1 class which is partial is inherited from an Interface but I found the typecasting error on this: My interface class ImedicalGroup.cs

public partial interface IMedicalGroup : IAudit
{
    int Id { get; set; }
    string Name { get; set; }
}

My Child partial class medicalgroups.cs

 public partial class MedicalGroups : IMedicalGroup,IAudit
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
#Audit region
}

My 2nd partial class of medicalgroups.cs in QueryableDbContext.cs

[global::System.Data.Services.Common.DataServiceKeyAttribute("Id")]
public partial class MedicalGroups
{
....
}

But I have found error on this line is "Cannot implicitly convert type 'HRBC.BusinessDataContext.MedicalGroups' to 'HRBC.Domain.Entities.IMedicalGroup'. An explicit conversion exists (are you missing a cast?)"

IMedicalGroup IClient.MedicalGroup { get { return MedicalGroup; } }

Solution

    1. Are you sure your interface needs to be partial?
    2. If you implement IMedicalGroup, you don't need to specify IAudit too, since IMedicalGroup already forces you to implement IAudit
    3. Make sure, that your child partial classes are in the same namespace! ( namespace X.Y.Z { ... })!

    Cheers! :)