How to fix FirstOr Default error in EF Core and how to map selected tags to BlogPost in ASP.Net why find returns null in ef core
00:39 18 Jan 2026
namespace Bloggie.Web.Controllers
  
{
  
    public class AdminBlogPostsController : Controller
  
    {
  
        private readonly ITagRepository tagRepository;
  
        private readonly IBlogPostsRepository blogRepository;
  
        public AdminBlogPostsController(ITagRepository tagRepository, IBlogPostsRepository blogRepository)
  
        {
  
            this.tagRepository = tagRepository;
  
            this. blogRepository = blogRepository;
  
        }
  

  
        public IBlogPostsRepository BlogRepository { get; }
  

  
        \[HttpGet\]
  
        public async Task\ Add()
  
        {
  

  
            var tags = await tagRepository.GetAllAsync();
  
            //retriving all Names in Tag table
  
            //many dropdown options
  
            var Model = new AddBlogPostRequest
  
            {
  
                Tags = tags.Select(x =\> new SelectListItem { Text = x.Name, Value = x.Id.ToString() })
  
            };
  

  

  
            return View(Model);
  

  
        }
  
        \[HttpPost\]
  
        public async Task\ Add(AddBlogPostRequest addBlogPostRequest)
  
        {
  
            //Map ViewModel to DomainModel
  
            var blog = new BlogPost
  
            {
  
                Heading= addBlogPostRequest.Heading,
  
                PageTitle= addBlogPostRequest.PageTitle,
  
                Content= addBlogPostRequest.Content,
  
                ShortDescription= addBlogPostRequest.ShortDescription,
  
                FeaturedImageUrl= addBlogPostRequest.FeaturedImageUrl,
  
                UrlHandle= addBlogPostRequest.UrlHandle,
  
                PublishedDate= addBlogPostRequest.PublishedDate,
  
                Author= addBlogPostRequest.Author,
  
                Visible= addBlogPostRequest.Visible,
  

  

  

  
            };
  
            var selectedTags = new List\();
  
            //Map tags from selected tags
  
            foreach(var selectedtagsId in addBlogPostRequest.SelectedTags)
  

  
            {
  
                var selectedTagIdAsGuid = Guid.Parse(selectedtagsId);
  
                //Fetch from database
  
                 var existingtag=await tagRepository.GetTagAsync(selectedTagIdAsGuid);
  
                if (existingtag != null)
  
                {
  
                    selectedTags.Add(existingtag);
  
                    
  
                }
  

  

  

  
            }
  
            //Mapping tags back to domainModel
  
            blog.Tag = selectedTags;
  

  

  

  
            await blogRepository.AddAsync(blog);
  
            
  
            return RedirectToAction("Add");
  

  

  
        }  

  

  
    }
  
}
using Microsoft.AspNetCore.Mvc.Rendering;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace Bloggie.Web.Models.ViewModels
{
    public class AddBlogPostRequest
    {
        public string Heading { get; set; }
        public string PageTitle { get; set; }
        public string Content { get; set; }
        public string ShortDescription { get; set; }
        public string FeaturedImageUrl { get; set; }
        public string UrlHandle { get; set; }
        public DateTime PublishedDate { get; set; }
        public string Author { get; set; }

        public bool Visible { get; set; }

        //IEnumerable → many items
       // SelectListItem → okka option
       // Together →
        public IEnumerable Tags {  get; set; }

        //stores User selected values in this 
        public string[] SelectedTags { get; set; }=Array.Empty();
    }
}

namespace Bloggie.Web.Models.Domain
{
    public class Tag
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string DisplayName { get; set; } 

        

        public ICollection BlogPost { get; set; } //one tag can have multiple BlogPosts
    }
}
c# model-view-controller