Ejemplo n.º 1
0
        public async Task SeedAsync()
        {
            _ctx.Database.EnsureCreated();
            var user = await _userManager.FindByEmailAsync("*****@*****.**");

            if (user == null)
            {
                user = new Member()
                {
                    UserName    = "******",
                    Email       = "*****@*****.**",
                    FirstName   = "Andrei",
                    LastName    = "Giurgiu",
                    Description = "Thirsty Nerd",
                    GoodreadsId = "18601222"
                };

                var result = await _userManager.CreateAsync(user, "P@ssw0rd!");

                if (result != IdentityResult.Success)
                {
                    throw new InvalidOperationException($"Could not create new user. [result={result.Errors.FirstOrDefault()}]");
                }
            }

            if (!_ctx.Books.Any())
            {
                user = _ctx.Members.FirstOrDefault();
                var filePath = Path.Combine(_hosting.ContentRootPath, "Data/library.json");
                var json     = File.ReadAllText(filePath);

                var books = JsonConvert.DeserializeObject <IEnumerable <Book> >(json).ToList();
                _ctx.Books.AddRange(books);
                _ctx.Members.AddRange();

                var group = _ctx.Groups.FirstOrDefault(g => g.Id == 1);
                if (group != null)
                {
                    var groupMember = new GroupMember()
                    {
                        Member   = user,
                        Group    = group,
                        IsAdmin  = true,
                        GroupId  = group.Id,
                        MemberId = user.Id
                    };
                    group.Members = new List <GroupMember>()
                    {
                        groupMember
                    };

                    var book    = books.FirstOrDefault();
                    var meeting = new Meeting()
                    {
                        Book    = book,
                        Address = "1050 Harwood",
                        Time    = DateTime.UtcNow.AddDays(14),
                        GroupId = group.Id,
                    };
                    var meetingMember = new MeetingMember()
                    {
                        IsHost    = true,
                        Meeting   = meeting,
                        MeetingId = meeting.Id,
                        Member    = user,
                        MemberId  = user.Id
                    };
                    meeting.Attendees = new List <MeetingMember>()
                    {
                        meetingMember
                    };
                    group.Meetings = new List <Meeting> {
                        meeting
                    };
                    user.Memberships = new List <GroupMember>()
                    {
                        groupMember
                    };
                    user.Attendances = new List <MeetingMember>()
                    {
                        meetingMember
                    };
                }

                _ctx.SaveChanges();
            }
        }
Ejemplo n.º 2
0
 public bool SaveAll()
 {
     return(_ctx.SaveChanges() > 0);
 }