This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var posts = context.Posts.ToArray(); | |
| var model = (from post in posts | |
| select new PostsViewModel | |
| { | |
| Title = post.Title, | |
| Url = post.Url, | |
| AuthorName = post.Author.Name, | |
| AuthorTwitterHandle = post.Author.TwitterHandle | |
| }).ToArray(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Uses string notation - lots of typos! | |
| //var posts = context.Posts | |
| // .Include("Author") | |
| // .ToArray(); | |
| //Uses lambda notation - Intellisense and easy refactoring = love | |
| //Note: include System.Data.Entity to use. | |
| var posts = context.Posts | |
| .Include(p => p.Author) | |
| .ToArray(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var posts = context.Posts | |
| .Include(p => p.Author.JobTitle) //We are going two levels deep | |
| .ToArray(); | |
| //We are going two levels deep here, also. However, since Authors.Posts is a collection, we | |
| //need to do a little LINQ-Fu to properly include Categories. | |
| var author = context.Authors | |
| .Include(a => a.Posts.Select(p => p.Category)) | |
| .Single(a => a.ID == id); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| kick |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE TABLE [dbo].[Authors]( | |
| [ID] [int] IDENTITY(1,1) NOT NULL, | |
| [Name] [nvarchar](max) NULL, | |
| [TwitterHandle] [nvarchar](max) NULL, | |
| [JobTitle_ID] [int] NULL, | |
| CONSTRAINT [PK_dbo.Authors] PRIMARY KEY CLUSTERED | |
| ( | |
| [ID] ASC | |
| )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] | |
| ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Data.Entity; | |
| using System.Data.Entity.ModelConfiguration; | |
| using System.Data.Entity.ModelConfiguration.Conventions; | |
| using EntityFrameworkExtraMile.Web.Domain.Model; | |
| namespace EntityFrameworkExtraMile.Web.DataAccess | |
| { | |
| public class BlogContext : DbContext | |
| { | |
| public BlogContext() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE TABLE [dbo].[Author]( | |
| [ID] [int] IDENTITY(1,1) NOT NULL, | |
| [Name] [nvarchar](50) NOT NULL, | |
| [TwitterHandle] [nvarchar](50) NULL, | |
| [JobTitleID] [int] NULL, | |
| CONSTRAINT [PK_dbo.Author] PRIMARY KEY CLUSTERED | |
| ( | |
| [ID] ASC | |
| )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] | |
| ) ON [PRIMARY] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [Table("Author")] | |
| public class Author : EntityBase | |
| { | |
| public Author() | |
| { | |
| Posts = new Collection<Post>(); | |
| } | |
| [Required] | |
| [StringLength(50)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace EntityFrameworkExtraMile.Web.Migrations | |
| { | |
| using System; | |
| using System.Data.Entity; | |
| using System.Data.Entity.Migrations; | |
| using System.Linq; | |
| internal sealed class Configuration : DbMigrationsConfiguration<EntityFrameworkExtraMile.Web.DataAccess.BlogContext> | |
| { | |
| public Configuration() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace EntityFrameworkExtraMile.Web.Migrations | |
| { | |
| using System; | |
| using System.Data.Entity.Migrations; | |
| public partial class InitialCreate : DbMigration | |
| { | |
| public override void Up() | |
| { | |
| CreateTable( |
OlderNewer