1

I'm new to ASP.NET MVC and I'm trying to make a simple application where I only have two tables, the first table is the author and the second is the table books. When I register an author, he saves it in the database, but when I want to register a book, he marks me an error.

System.Data.Entity.Infrastructure.DbUpdateException: 'An error occurred while updating the entries. See the inner exception for details.'

InvalidOperationException: A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'Id'.

I have a foreign key that refers to authors in the book table:

Id_autor int foreign key references autor(ID)

And this is the POST method

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Id,nombrelibro")] libros libros)
    {
        if (ModelState.IsValid)
        {
            db.libros.Add(libros);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.Id = new SelectList(db.autor, "Id", "nombre", libros.Id);
        return View(libros);
    }

I tried to do the following

if (ModelState.IsValid)
{
    libros.autor.Id = 1; 
    db.libros.Add(libros);
    b.SaveChanges();
    return RedirectToAction("Index");
}

But it also gives me the following error: System.NullReferenceException: 'Object reference not set as instance of an object

nexer13
  • 11
  • 3
  • Try this link for more info https://stackoverflow.com/questions/6384659/a-dependent-property-in-a-referentialconstraint-is-mapped-to-a-store-generated-c – r lo Aug 01 '18 at 13:13

0 Answers0