1

I have two interface:

public interface IPerson
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public ILocation LocationOfBirth { get; set; }
}
public interface ILocation
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

Now I want to implement them like this:

public class Location : ILocation
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}
public class Person : IPerson
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Location LocationOfBirth { get; set; }
}

But c# is angry on me:

Property 'LocationOfBirth' cannot implement property from interface 'IPerson'. Type should be 'ILocation'.

Why is this so when Location fulfills the all requirements of ILocation?

I want to use Person as a model for EntityFramework, so I cannot use ILocation. What to do?


Edit: In my application, the implementations of the interfaces are not in the scope of the interfaces, so I cannot define IPerson with the Location implementation.

6
  • 2
    Why do you have IPerson and ILocation interfaces defined in the first place? What value are they providing you? Commented Dec 30, 2021 at 15:06
  • This is a minimal example. The goal is to implement different models of Person for different databases. So I have to use different flags and converters. But in my main application, I just want to use the interface. This way, I hope I can switch databases quickly. Commented Dec 30, 2021 at 15:10
  • Try removing the set; from the properties in your interfaces (keep them in your implementing classes). Commented Dec 30, 2021 at 15:11
  • I can remove the "set;", but it does not seem to make a difference for my problem. Commented Dec 30, 2021 at 15:14
  • Ah, I misunderstood the error message. You have to change the type to ILocation on LocationOfBirth property in your Person class, there's no way around that as the types have to match the interface exactly. This shouldn't be an issue with entity framework though since Location implements ILocation. Commented Dec 30, 2021 at 15:22

3 Answers 3

1

if you want to use your class in EF try this code. EF will have a concrete class version, and if you need an interface somewhere else, it will be working too.

public class Person : IPerson
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    
    [NotMapped]
    public ILocation LocationOfBirth 
    {
        get {return BirthLocation;}
        set {BirthLocation= (Location) value; 
    }
    public Location BirthLocation {get; set;}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It worked without the [NotMapped] flag. I don't know, if this is a new feature by .NET6 or if I just misunderstand the conception if EF. You and Zohar solved my problem. Thanks!
0

The c# precompiler resolves ILocation to the concrete class Location upon instantiation or assignment during usage

public class Person : IPerson
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public ILocation LocationOfBirth { get; set; }
}

Comments

0

This can be done easily if you use explicit interface implementation:

public class Person : IPerson
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Location LocationOfBirth { get; set; }
    // Explicit interface implementation here
    ILocation IPerson.LocationOfBirth {
        get => this.LocationOfBirth; 
        set => this.LocationOfBirth = value as Location;
    }
}

2 Comments

Interesting! Can this be used with EF Core? In this case I have to force EF not to map the explicit interface implementation with [NotMapped]?
I have no idea. I don't work with EF. BTW, note that I've updated the code a little bit as it was written directly here so no intellisense...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.