本人在學(xué)習(xí)WebApi MVC中遇見(jiàn)一個(gè)問(wèn)題,求教.我在數(shù)據(jù)庫(kù)中建立的兩個(gè)表:books 和 bookPath,并且在這兩個(gè)表之間加了外鍵constraint,一對(duì)多的關(guān)系。(1book 對(duì) 多bookPath)在C#的工程中我使用codefist用VS2017自動(dòng)映射生成了表的class,并且我創(chuàng)建了一個(gè)bookModel包含了這兩個(gè)class中的所有字段。我在post方法中,應(yīng)該如何插入數(shù)據(jù)呢?我的post代碼如下:public void PostBook( [FromBody] BookModel bookModel){try {using (BooksDBEntities enbook = new BooksDBEntities()){Book book = new Book(){bookName = bookModel.bookName};enbook.Books.Add(book);enbook.SaveChanges();BookPath path = new BookPath(){bookID = bookModel.bookID,pathAddress = bookModel.pathAddress};enbook.BookPaths.Add(path);enbook.SaveChanges();}}catch (DbEntityValidationException dbEx) {foreach (var validationErrors in dbEx.EntityValidationErrors){foreach (var validationError in validationErrors.ValidationErrors){Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);}}}}每次只能給book表中插入數(shù)據(jù)。bookpath不能執(zhí)行,請(qǐng)各位幫忙指點(diǎn)一下
- 1 回答
- 0 關(guān)注
- 883 瀏覽
添加回答
舉報(bào)
0/150
提交
取消