0

I am new in ASP .Net MVC

I have three table, Categories, Intents and IntentCategories.

IntentCategories table has two columns as IntentId and CategoryId. I display the names instead of Ids.

This is my IntentCategories Model.

  {
    public Guid Id { get; set; }
    public Guid IntentId { get; set; }
    public Guid CategoryId { get; set; }

    [NotMapped]
    public virtual string CategoryName { get; set; }
    public virtual Categories Category { get; set; }
    public virtual string IntentFriendlyName { get; set; }
    public virtual Intents Intent { get; set; }
}

And here these are my Create methods

 public IActionResult Create()
    {
        List<SelectListItem> selectListItemsIntents = _context.Intents.
                                                    Select(i => new SelectListItem()
                                                    {
                                                        Text = i.FriendlyName,
                                                        Value = i.Id.ToString()
                                                    }).ToList();

        List<SelectListItem> selectListItemsCategories = _context.Categories.
                                                        Select(m => new SelectListItem()
                                                        {
                                                            Text = m.Name,
                                                            Value = m.Id.ToString()
                                                        }).ToList();

        ViewBag.selectedListItemsIntents = selectListItemsIntents;
        ViewBag.selectListItemsCategories = selectListItemsCategories;
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
 public async Task<IActionResult> Create([Bind("Id,IntentId,CategoryId")] IntentCategories intentCategories)
    {
        if (ModelState.IsValid)
        {
            var intent = _context.Intents.Where(x => x.Id == intentCategories.IntentId).FirstOrDefault();
            
            intentCategories.Intent = intent;

            var category = _context.Categories.Where(m => m.Id == intentCategories.CategoryId).FirstOrDefault();
            intentCategories.Category = category;

            intentCategories.Id = Guid.NewGuid();
            
            _context.Add(intentCategories);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(intentCategories);
    }

I have create page, I see the name of Intent and name of Category, Its Ok, I can select them. When I click the create button I get an error: Invalid column name: IntentFriendlyName and CategoryName.

How can I fix this? I use these two column just display the name instead of ID.

I edited the question make more clear. When I clik the create button CategoryID,IntentID and IntentCategoriesId are created. These are enough for me. Other null columns are not exist in my table, I use them just displaying name.

enter image description here

simoncare
  • 75
  • 1
  • 8
  • Did you run migrations? The tables might not exist – Qudus Feb 11 '21 at 11:25
  • I am working on existing datebase. The tables exist. – simoncare Feb 11 '21 at 11:26
  • 1
    Like the error message is saying, you have a property named `IntentFriendlyName` in the model, but not in the database, so when it tries to insert the value of `IntentFriendlyName ` to the column with saem name, it fails. You need to set `NotMapped` attribute for that field too. – Mat J Feb 11 '21 at 11:49
  • I have NotMapped attribute in my IntentCategories model. You can see the above, in first code block – simoncare Feb 11 '21 at 11:51
  • ooov Ok, I got the point @MatJ thanks you :) – simoncare Feb 11 '21 at 11:59
  • Try using Lazy Loader from EF [this post](https://stackoverflow.com/a/51707911/11443424) has an example! – vini b Jun 28 '22 at 17:03

0 Answers0