我有两张桌子:
表1 - MainTable
---------------------------------------
| MainTableID | CustomerName | BookID |
---------------------------------------
表2 - BookTable
----------------------
| BookID | BookName |
----------------------
| 1 | physics |
----------------------
| 2 | Math |
----------------------
我想得到这样的结果:
---------------------------------------
| MainTableID | CustomerName | BookID |
---------------------------------------
| 1 | Alex | Math |
---------------------------------------
我在BookTable中有BookNames列表,我想在MainTable中插入数据。我在visual studio中使用ADO.NET实体数据模型,我这样做:
BookTable correspondingBook=(from row in entities.BookTable
where rows.BookName == "Math"
select rows).First();
MainTable itemToAdd = new MainTable();
itemToAdd.CustomerName = "Alex";
itemToAdd.BookID = correspondingBook.BookID;
entities.MainTable.Add(itemToAdd);
entities.SaveChanges();
这个问题是否很好?如果不是,哪个更好?