2

I have a table with books and a table with authors created by model-first. There's a navigation property in Books table pointing to AuthorID (because each book has an author) here i'm trying to fill in the tables with values and it's okay when it comes down to scalar properties. But how to add a value to navigation property? My navigation property is called AuthorBookenter image description here

 Book book1 = new Book();
 Author author1 = new Author();
author1.Surname = "Dickens";
 author1.Name = "Charles";
 db.AuthorSet.Add(author1);`
hbook1.Title = "Great Expectations";
book1.Genre = "Novel"
db.BookSet.Add(book1);
 db.SaveChanges();

'

David Shepard
  • 181
  • 2
  • 10

2 Answers2

1

try something like this:

var rel = new AuthorBook { Book = book1, Author = author1 };
book1.AuthorBook.Add(rel);

that obviously goes before SaveChanges();

Z .
  • 12,657
  • 1
  • 31
  • 56
  • And how to fill in another navigation property? the connection is One-to-many Many books in one location I have created location already `Location location1 = new Location(); location1.Floor = "2"; location1.Cupboard = "3"; location1.Section = "1"; location1.Shelf = "2"; location1.Row = "1"; db.LocationSet.Add(location1);` I tried to fill in location the same way as you suggested to write AuthorBook but visual studio underlines it with red. And AuthorBook is nor underlined, which is good – David Shepard Dec 27 '16 at 19:13
  • if the location exists you'll need to select it first and then add it to the book (as the author). – Z . Dec 27 '16 at 19:17
  • But I can't add it by writing the same code as for the author – David Shepard Dec 27 '16 at 19:20
  • var location = db.Locations.Where(x => x.Floor == floor && x.Cupboard == cupboard && ...).SingleOrDefault(); if (location == null) location = new Location { Floor = floor, Cupboard = cupboard, ... }; book1.Location = location; – Z . Dec 27 '16 at 19:23
  • I added your suggested code (but location instead of locations) and it says it can't resolve symbol Location – David Shepard Dec 27 '16 at 19:41
  • This answer tackles your *original* question. You shouldn't "hijack" an answerer by adding new questions in comments. – Gert Arnold Dec 27 '16 at 21:41
0
Book book1 = new Book();
book1.Title = "Great Expectations";
book1.Genre = "Novel"

// Add 1 or more authors
Author author1 = new Author();
author1.Surname = "Dickens";
author1.Name = "Charles";

// AuthorBook better be a collection since a book can have 1 or many authors
book1.AuthorBook.Add(author1); 

db.BookSet.Add(book1);
db.SaveChanges();

The above code will create one record in Book table, 1 in Author and one in AuthorBook. You do not need to manually add an item in code to AuthorBook since EF will take care of that for you.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • No, if i write like that `book1.AuthorBook.Add(author1);` visual studio underlines author1 – David Shepard Dec 27 '16 at 19:17
  • underlines and says what? – CodingYoshi Dec 27 '16 at 19:38
  • Argument Type "WpfApplication1.Author" is not assignable to parmeter type "WpfApplication1.AuthorBook" – David Shepard Dec 27 '16 at 19:43
  • 1
    That means your model is set up incorrectly. As I said in my comments, your `Book` should have a navigation property which is a collection of `Author` types. Otherwise your model is not correct. You have a many to many here, so read [this](http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx) for more info. – CodingYoshi Dec 27 '16 at 20:08
  • @CodingYoshi The name of the navigation property should be `Authors`, the `AuthorBook` type/table is not really a domain concept it is just relational boilerplate. – Aluan Haddad Dec 27 '16 at 20:21
  • Then add it to that – CodingYoshi Dec 27 '16 at 20:42
  • So does that answer your question? – CodingYoshi Dec 27 '16 at 21:35
  • @CodingYoshi No, the model is set up correctly. Book should *not* have a collection of Authors. That would mean that any author can only have one book. Frankly, I don't understand why the other answer isn't marked as accepted. – Gert Arnold Dec 29 '16 at 08:40
  • @GertArnold Just because Book has a collection of Authors does not mean that an author can have one book-they can have many books. See [this](http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx) – CodingYoshi Dec 30 '16 at 01:31