コード例 #1
0
        public async static Task Seed(this IApplicationBuilder app, string path)
        {
            PhotoSharingContext context = app.ApplicationServices.GetService<PhotoSharingContext>();
            context.Database.Migrate();

            await addPhoto(context, new Photo {
                Title = "Me standing on top of a mountain",
                Description = "I was very impressed with myself",
                UserName = "******",
                PhotoFile = getFileBytes($@"{path}\Images\flower.jpg"),
                ImageMimeType = "image/jpeg",
                CreatedDate = DateTime.Today,
                Comments = new List<Comment>(){
                        new Comment {
                            UserName = "******",
                            Subject = "A Big Mountain",
                            Body = "That looks like a very high mountain you have climbed"
                        },
                        new Comment {
                            UserName = "******",
                            Subject = "So?",
                            Body = "I climbed a mountain that high before breakfast everyday"
                        }
                    }
            });

            await addPhoto(context, new Photo {
                Title = "My New Adventure Works Bike",
                Description = "It's the bees knees!",
                UserName = "******",
                PhotoFile = getFileBytes($@"{path}\Images\orchard.jpg"),
                ImageMimeType = "image/jpeg",
                CreatedDate = DateTime.Today,
                Comments = new List<Comment>(){
                        new Comment {
                            UserName = "******",
                            Subject = "Jealous",
                            Body = "Wow, that new bike looks great!"
                        }
                    }
            });

            await addPhoto(context, new Photo {
                Title = "View from the start line",
                Description = "I took this photo just before we started over my handle bars.",
                UserName = "******",
                PhotoFile = getFileBytes($@"{path}\Images\path.jpg"),
                ImageMimeType = "image/jpeg",
                CreatedDate = DateTime.Today
            });

            await context.SaveChangesAsync();
        }
コード例 #2
0
 private static async Task addPhoto(PhotoSharingContext context, Photo toAdd) {
     var found = await context.Photos.FirstOrDefaultAsync(p => p.Title == toAdd.Title);
     if (found == null) context.Photos.Add(toAdd);
 }