Beispiel #1
0
        public static void Main(string[] args)
        {
            FileAdapter fileAdapter = new FileAdapter();
            var         allFamilies = fileAdapter.getAllFamilies();


            //AddAllFamilies(allFamilies);

            using (FamilyDbContext familyDbContext = new FamilyDbContext())
            {
                var           first         = familyDbContext.Childs.First(child => child.FirstName.Equals("Maria"));
                Interest      interest      = familyDbContext.Interests.First(interst => interst.Type.Equals("Fodbold"));
                ChildInterest childInterest = new ChildInterest()
                {
                    Child    = first,
                    Interest = interest
                };

                familyDbContext.ChildInterests.Add(childInterest);
                familyDbContext.SaveChanges();
            }



            CreateHostBuilder(args).Build().Run();
        }
        private List <Contact> CreateTestContactsWithDetails(FamilyDbContext context)
        {
            // Create test entities
            var category1 = new ContactCategory
            {
                CategoryName = "Friends",
            };
            var contact1 = new Contact
            {
                ContactCategory = category1
            };

            // Persist entities
            context.Contacts.Add(contact1);
            context.SaveChanges();

            // Create one-to-one related entity
            var data1 = new ContactData
            {
                Data      = "Data 1",
                ContactId = contact1.Id,
                Contact   = contact1
            };

            context.ContactDatas.Add(data1);
            context.SaveChanges();

            // Detach entities
            var objContext = ((IObjectContextAdapter)context).ObjectContext;

            objContext.Detach(contact1);

            // Clear reference properties
            contact1.ContactCategory = null;
            contact1.ContactData     = null;

            // Return entities
            return(new List <Contact> {
                contact1
            });
        }
        public Token Authenticate(string login, string password)
        {
            // authentication by the Login and Password
            var now = CommonService.Now;

            Token result = null;

            // создаем новый контекст, дабы не использовать "уже загруженные" сущности
            using (var dbContext = new FamilyDbContext())
            {
                UserToken userToken = null;

                // authentication for Managers
                if (userToken == null)
                {
                    // находим пользователя по логину
                    var manager = dbContext.Set <Manager>()
                                  .Include(t => t.Person.Avatar)
                                  .Include(t => t.Roles)
                                  //.Where(m => m.Person.State == Common.Enums.ObjectState.Active)
                                  .FirstOrDefault(m => m.Login.ToLower() == login.ToLower());

                    if (manager != null && CommonService.VerifyPassword(password, manager.Password))
                    {
                        userToken = UserToken.Create(manager, null, now.AddHours(DefaultExpirationHours));
                    }
                }

                // authentication for Guests
                if (userToken == null)
                {
                    var guest = dbContext.Set <Guest>()
                                .FirstOrDefault(g => g.Date <= now && now <= g.Expire && g.Login.ToLower() == login.ToLower());

                    if (guest != null && CommonService.VerifyPassword(password, guest.Password))
                    {
                        userToken = UserToken.Create(null, guest, guest.Expire);
                    }
                }

                // saving new token
                if (userToken != null)
                {
                    dbContext.Set <UserToken>().Add(userToken);
                    dbContext.SaveChanges();

                    result = this.BuildToken(userToken);
                }
            }

            return(result);
        }
Beispiel #4
0
 private static void AddAllFamilies(IList <Family> allFamilies)
 {
     using (FamilyDbContext familyDbContext = new FamilyDbContext())
     {
         for (int i = 0; i < allFamilies.Count; i++)
         {
             familyDbContext.Families.Add(new Family()
             {
                 HouseNumber = allFamilies[i].HouseNumber,
                 StreetName  = allFamilies[i].StreetName
             });
             familyDbContext.SaveChanges();
         }
     }
 }