Anuj Chauhan
Anuj Chauhan

Reputation: 3

When using Explicit Interface Implementation in Entity classes , EF Core does not creates column in table

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

    }
public class CareerApplication : IRecordInformation
    {
        public int CareerApplicationId { get; set; }
        public string Name { get; set; }
        public string FileName { get; set; }
        public string Email { get; set; }
        public DateTime IRecordInformation.CreatedOn { get; set; }
        public  DateTime IRecordInformation.ModifiedOn { get; set; }
    }

Why i am doing this ? Because if i change the interface and remove a property then there should be compile time error that there is no property declared in interface for which the implementation exist in class . This way i can remove the implementation from class . But if i dont use explicit implementation using interface name in class then if i remove a property from interface then that corresponding property will be treated as the property of class itself.

I have tried doing like

public DateTime CreatedOn { get => ((IRecordInformation)this).CreatedOn; set => ((IRecordInformation)this).CreatedOn = value; }

But there is a stack overflow exception for which i am attaching the image

Error i am facing

Upvotes: 0

Views: 854

Answers (1)

Anuj Chauhan
Anuj Chauhan

Reputation: 3

It works as suggested by @Gert Arnold

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;
            }

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

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

        }
        bool IPublishInformation.IsPublished { get; set; }

On my blog you can find out the complete problem i was facing in my project http://blogs.anujchauhan.com/post/2020/05/01/Implicit-to-Explicit-implementations-of-interface-in-class-for-Entity-Framework-Core-Common-Columns-in-table-like-CreatedOn-UpdatedOn-DeletedOn-IsPublished

Upvotes: 0

Related Questions