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
a95e4f18-bf26-444e-892d-66749bb88dce|0|.0|96d5b379-7e1d-4dac-a6ba-1e50db561b04
ASP.NET MVC Core, C#
interface, explicit implementation of interface, implicit implementation of interface, implicit to explicit implementation, CreatedOn, ModifiedOn, DeletedOn, IsPublished, UpdatedOn