Implicit to Explicit implementations of interface in class for Entity Framework Core Common Columns in table like CreatedOn UpdatedOn DeletedOn IsPublished

1. May 2020

Question

I want to make some properties common to entity classes which can be implemented in class. These properties can be selective in nature means some class has one of them or more of them or none. So in order to do that we need multiple inheritance , which can only be implemented by the use of interfaces.

What I searched on internet :

Why entity framework core is not creating columns for the explicit implementations of interface properties

I have created two interfaces with the their properties

 

public interface IRecordInformation
    {
         public DateTime CreatedOn { get; set; }
         public DateTime ModifiedOn { get; set; }

    }
public interface IPublishInformation
    {
        public bool IsPublished { get; set; }
        
    }

Now suppose that I have created the Entity Class as Service and inherited both the interfaces . I have implemented the properties of interfaces which needs to be done . So I got a system where in I can just inherit the interfaces which I need in any class and the common properties will be forced by compiler with an error if I don’t implement them .

 
public class Service : IRecordInformation, IPublishInformation
    {
        public int ServiceId { get; set; }
        public string Name { get; set; }
        public string FileName { get; set; }
        
        public DateTime CreatedOn { get; set; }
        public  DateTime ModifiedOn { get; set; }
        public bool IsPublished { get; set; }
    }

We have achieved avoiding the mistake of forgetting to include CreatedOn ModifiedOn Properties in any class . Now the question is if I want to remove the modifiedOn property from all the class will removing it from interface will work ? answer is no because as soon as I remove the property from interface the compiler thinks that the modifiedOn property is of Service class’s own property which makes our requirement error prone . Hence , what we can do is use explicit implementation of interface properties in class

This can be done like the below example.

public class Service : IRecordInformation, IPublishInformation
    {
        public int ServiceId { get; set; }
        public string Name { get; set; }
        public string FileName { get; set; }
        
        public DateTime IRecordInformation.CreatedOn { get; set; }
        public  DateTime IRecordInformation.ModifiedOn { get; set; }
        public bool IPublishInformation.IsPublished { get; set; }
    }

Now we have achieved that whenever there is any change in interface the compiler will force us to change the class implementation as well . But the problem arises now is the Entity Framework Core does not processes the explicit implementations of interface into table columns . So we have to make implicit to explicit implementations like below .

public bool IsPublished
        {
            get
            {
                return ((IPublishInformation)this).IsPublished;
            }
            set
            {
                ((IPublishInformation)this).IsPublished = value;
            }

        }

bool IPublishInformation.IsPublished { get; set; }
public DateTime CreatedOn 
   { 
       get 
            {
                ((IRecordInformation)this).CreatedOn;
            }
       set
            {
                 ((IRecordInformation)this).CreatedOn = value; 
            }
   }
public DateTime ModifiedOn
   {
         get
              {
                  return ((IRecordInformation)this).ModifiedOn;
              }
         set
              {
                  ((IRecordInformation)this).ModifiedOn = value;
               }

    }
public DateTime IRecordInformation.CreatedOn { get; set; }
public DateTime IRecordInformation.ModifiedOn { get; set; }

Now entity Framework core sees the CreatedOn ModifiedOn properties and creates the corresponding columns in the table .

Question on stackoverflow also 

https://stackoverflow.com/questions/61422152/when-using-explicit-interface-implementation-in-entity-classes-ef-core-does-no/61537924#61537924

ASP.NET MVC Core, C# , , , , , , , ,