Linq to Sql Foreign key relationships

白昼怎懂夜的黑 提交于 2019-12-11 03:46:30

问题


Table name: Author

  • AuthorID -> primary key
  • FirstName
  • LastName

Table name: Titles

  • ISBN -> primary key
  • BookTitle
  • EditionNumber
  • CopyRight

Table name: AuthorISBN

  • ISBN -> foreign key
  • AuthorID -> foreign key

How come the below code block below does not trigger the intellisense, since Linq-to-SQL automatically creates properties based on foreign-key relationship? In my case it doesn't...

ERROR:

The name title does not exist in current context in title.AuthorISBN

It's not letting me add a photo, but 'Author' has 'one-to-many' relationship with AuthorISBN, and 'Titles' also has 'one-to-many' relationship with AuthorISBN

Code:

 BooksDataContext database = new BooksDataContext();
 var authorsAndTitles =
          from title in database.Titles
          from book in title.AuthorISBN
          let author = book.Author
          orderby author.LastName, aurthor.FirstName, title.BookTitle
          select new { author.FirstName, author.LastName, title.BookTitle };

回答1:


It seems like you made a typo:

 var authorsAndTitles =
          from title in database.Titles
          from book in **tilte**.AuthorISBN
          let author = book.Author
          orderby author.LastName, aurthor.FirstName, title.BookTitle
          select new { author.FirstName, author.LastName, title.BookTitle };

Shouldn't it be title.AuthorISBN insteand of tilte.AuthorISBN? I surrounded the word with **.

Besides that, I would try this:

var authorsAndTitles =
          from title in database.Titles
          let author = title.AuthorISBN.Author
          orderby author.LastName, aurthor.FirstName, title.BookTitle
          select new { author.FirstName, author.LastName, title.BookTitle };

New attempt:

var authorsAndTitles =
          from autISBN in database.AuthorISBN
          let author = autISBN.Author
          let title = autISBN.Titles
          orderby author.LastName, author.FirstName, title.BookTitle
          select new { author.FirstName, author.LastName, title.BookTitle };


来源:https://stackoverflow.com/questions/12501285/linq-to-sql-foreign-key-relationships

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!