0

Hello,

I have tables two tables, the "book" and the "author", a book can have many authors and vice versa, so there is an intermediate table called "book_author" with two columns as foreign key for those two primary keys

book_author
book_id
author_id

I'm trying to edit the book details and also its author using the entity date model,

public ActionResult Edit(int id, FormCollection formValues)
        {
            var book = storeDB.books.Single(a => a.book_id == id);
            //var authors = storeDB.books.Single(a => a.book_id == id).authors;
            try
            {
                // save the book
                UpdateModel(book, "Book");
               
                //how to update
                //UpdateModel(authors, "Book");
                storeDB.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                var viewModel = new StoreManagerViewModel
                {
                    Book = book,
                    Categories = storeDB.categories.ToList(),
                    Authors = storeDB.authors.ToList(),
                    SelectedAuthors = bookAuthors(id)
                };
                return View(viewModel);
            }
        }
<form action="/StoreManager/Edit/1" method="post">
       
        <fieldset>
 
            <legend>Edit book</legend>
           
 
           
            <div class="editor-label">
                <label for="Book_title">title</label>
            </div>
            <div class="editor-field">
                <input id="Book_title" name="Book.title" type="text" value="In the Buddha's Words" />
               
            </div>
            <div class="editor-label">
 
                <label for="Book_price">price</label>
            </div>
            <div class="editor-field">
                <input Length="5" id="Book_price" name="Book.price" type="text" value="50.00" />
               
            </div>
           
            <div class="editor-label">
                <label for="Book_description">description</label>
            </div>
 
            <div class="editor-field">
                <input id="Book_description" name="Book.description" type="text" value="Buddha's dhamma in his own words" />
               
            </div>
           
            <div class="editor-label">
                <label for="Book_paperback">paperback</label>
            </div>
            <div class="editor-field">
                <input id="Book_paperback" name="Book.paperback" type="text" value="4003" />
 
               
            </div>
           
            <div class="editor-label">
                <label for="Book_book_img_url">book_img_url</label>
            </div>
            <div class="editor-field">
                <input id="Book_book_img_url" name="Book.book_img_url" type="text" value="/Content/Images/Books/buddha_words.jpg" />
               
            </div>
           
            <div class="editor-label">
 
                <label for="Book_category_id">category_id</label>
            </div>
            <div class="editor-field">
                <select id="Book_category_id" name="Book.category_id"><option selected="selected" value="1">Science</option>
<option value="2">Art</option>
<option value="3">Spiritual</option>
</select>
               
            </div>
 
            <div class="editor-label">
                <label for="Book_authors">authors</label>
            </div>
            <div class="editor-field">
                <select id="Book_author_id" multiple="multiple" name="Book.author_id" size="10"><option selected="selected" value="1">Bikkhu</option>
<option value="2">Buddha</option>
<option value="3">William</option>
 
<option value="4">Anthony</option>
</select>
               
            </div>
 
            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>
 
    </form>

I can only update the book table, but not author that the book has or the book_author, I'm new to asp.net, I can't easily find a tutorial on this issue, I think the logic is to delete the author of the book first and then add the author for that book, but I don't know exactly about that linq and C# code. Can show me ? Thanks

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by dnanetwork
0

welcome Entity Framework my Friend..
it is not like Linq 2 SQL..

things in entity model works on the OOP principals..

in order to update Authors which contains the Foriegn key from
table book_author which has a primary key..

so this is a association..

Author.book_author = _context.book_author.FirstOrDefault(item => item.id.Equals(Author.authorID));

now you can update the authors..

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.