0

I added to more tables to my existing databases. The AspNetForum and AspNetPosts. Then I run the following command

Scaffold-DbContext 'Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=aspnet-my-app-20191022075020' 
         Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -f

In order to update my databases because first I thought that this was the problem.

I have primary keys. So the problem is not the primary keys. I've been searching for 2 days on the internet for a solution but I wasn't able to figure out what the problem is.

Do I need to update my databases again? Does anyone have a clue what's going on here?

My model classes for my database are the following:

namespace my_app.Models
{
    public partial class AspNetForum
    {
        [Key]
        public int topic_id { get; set; }
        public string topic_title { get; set; }
        public DateTime? topic_create_time { get; set; }
        public string topic_owner { get; set; }
    }

    public partial class AspNetPosts
    {
        [Key]
        public int post_id { get; set; }
        public int topic_id { get; set; }
        public string post_text { get; set; }
        public DateTime? post_create_time { get; set; }
        public string post_owner { get; set; }
    }
}

Controller:

[HttpPost]
public ActionResult AddTopic(AddTopic kati)
{
    if (ModelState.IsValid)
    {
        AspNetForum topic = new AspNetForum
                {
                    topic_title = kati.Topic_Title,
                    topic_create_time = DateTime.Now,
                    topic_owner = kati.Email
                };

        dbContext.AspNetForum.Add(topic);
        dbContext.SaveChangesAsync();

        int id = topic.topic_id;

        AspNetPosts fp = new AspNetPosts
                {
                    topic_id = id,
                    post_text = kati.Topic_Post,
                    post_create_time = DateTime.Now,
                    post_owner = kati.Email
                };

        dbContext.AspNetPosts.Add(fp);
        dbContext.SaveChangesAsync();
    }

    return View(kati);
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
john
  • 23
  • 6

1 Answers1

0

Try to call SaveChanges instead of SaveChangesAsync. This way you will be able to catch an exception if any.

details about SaveChanges vs SaveChangesAsync: Entity Framework SaveChanges() vs. SaveChangesAsync() and Find() vs. FindAsync()

Pavel Shastov
  • 2,657
  • 1
  • 18
  • 26