I have a hiring management system app with 2 entities Person & Tag.
Person can have a list of tags.
This is my person entity:
public int Id { get; set; }
public string FirstName { get; set; }
public string Family { get; set; }
public long NationalId { get; set; }
public DateTime BirthDate { get; set; }
public ICollection<TagAggregate> Tags { get; set; }
and this is my tag entity:
public int Id { get; set; }
public string TagName { get; set; }
public string Description { get; set; }
public int PersonId { get; set; }
public PersonAggregate Person { get; set; }
Everything works fine except when I want to search persons with their tag names.
For example, I have a person John, with a programmer and a designer tag.
When I search for these tags, it returns the first record and can't find the second one.
For example I created a tag name with value programmer for person id 1 (which is "john"), then I created another tag name with value designer (again for person id 1 ("john"))!
It only finds programmer (because I created it first) and returns 204 for "designer".
And when I remove "programmer" tag, then I can find john with tag name "designer".
I don't know what the problem is.
Here is my search method :
public async Task<PersonAggregate> FindByTagName(string tagName)
{
var person = DbSet.Where(p => p.Tags
.Select(t => t.TagName == tagName).FirstOrDefault())
.FirstOrDefault();
return person;
}