There are several simple classes:
The first class:
public class User
{
public int UserId { get; set; }
public string Username { get; set; }
// ...
public ICollection<Topic> Topics { get; set; }
}
The second class:
public class Publication
{
public int PublicationId { get; set; }
public string Title { get; set; }
/ ...
public User Author { get; set; }
}
The third class:
public class Topic: Publication
{
public string TopicContent { get; set; }
// ...
}
After creating a database for my model I have the following stucture of the database:
Users
UserId
UserName
Publications
PublicationId
Title
TopicContent
Author_UserId
User_UserId
As you can see I get two fields Author_UserId and User_UserId having identical role in the table Publications.
How can I merge this fields into one field using Fluent API or Data Annotation?
I don't think that it's possible to have the same foreign key column in the Publications
table. Publication.Author
and User.Topics
cannot be the endpoints of one and the same association. You could have a Publication
instance which isn't a Topic
and a reference to a User
:
User user = new User() { Topics = new List<Topic>() };
Publication publication = new Publication();
publication.Author = user;
user.Topics.Add(???);
At ??? you can't add publication
because it isn't a Topic
instance. user.Topics
must refer to another object than publication
which means that those endpoints cannot belong to the same association.
Edit
If you want only one single association with only a single foreign key column in the database you must either move the Author
property from Publication
to Topic
or let the collection in your User
class refer to Publication
instead of Topic
:
public ICollection<Publication> Publications { get; set; }