I'm trying to create a blog, so each post has some topic and vise versa (a many-to-many relationship). I've created two Interface
s and two Class
es, and I want to create database using EF Code First
solution. this is my code:
// interfaces
public interface IPost
{
int id { get; set; }
string name { get; set; }
IEnumerable<ITopic> topics { get; set; }
}
public interface ITopic
{
int id { get; set; }
string name { get; set; }
IEnumerable<IPost> posts { get; set; }
}
// classes
public class Post : IPost
{
public int id { get; set; }
public string name { get; set; }
// ... (other properties)
public virtual ICollection<Topic> topics { get; set; }
}
public class Topic : ITopic
{
int id { get; set; }
string name { get; set; }
// ... (other properties)
public virtual ICollection<Post> posts { get; set; }
}
But I get the following error:
'Topic' does not implement interface member 'ITopic.posts'. 'Topic.posts' cannot implement 'ITopic.posts' because it does not have the matching return type of 'IEnumerable<IPost>'.
And also, I get the (almost) same error for Post
.
I know that ICollection<T>
implements IEnumerable<T>
interface. and, as you see, Post
implements IPost
. So, why i get this error? I tried these also (whitin Topic
class):
1- public virtual ICollection<IPost> posts { get; set; }
2- public virtual IEnumerable<Post> posts { get; set; }
(why this one dose not work?!)
The only code that works is public virtual IEnumerable<IPost> posts { get; set; }
, but then I will lose the other propertis of a Post
when enumerating topic.posts
. worse, I can't create database using EF Code First
solution.
Is there any workaround to solve this problem?