Пример #1
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            UserIds  users = new UserIds();
            RolesIds roles = new RolesIds();

            modelBuilder.Entity <OrderProduct>()
            .HasKey(op => new { op.OrderId, op.ProductId });

            modelBuilder.Entity <User>()
            .HasMany(u => u.Orders)
            .WithOne(u => u.User)
            .HasForeignKey(u => u.UserId);

            modelBuilder.Entity <Order>()
            .HasMany(o => o.OrderProducts)
            .WithOne(o => o.Order)
            .HasForeignKey(o => o.OrderId);

            modelBuilder.Entity <Product>()
            .HasMany(p => p.OrderProducts)
            .WithOne(p => p.Product)
            .HasForeignKey(p => p.ProductId);

            modelBuilder.Entity <Invoice>()
            .HasOne(i => i.Order)
            .WithMany();

            modelBuilder.SeedUsers(roles, users);
            modelBuilder.SeedProducts();
            modelBuilder.SeedOrders(users);
            modelBuilder.SeedOrderProducts();
        }
        public bool IsValid()
        {
            if (!RolesIds?.Any() ?? true)
            {
                throw new Exception("O campo Permissões é obrigatório.");
            }
            if (!Contacts?.Any(contact => contact.ContactType == ContactType.Email) ?? true)
            {
                throw new Exception("Pelo um Email de contato deve ser informado.");
            }

            return(true);
        }
        public User GetUser(MainContext db)
        {
            var user = new User
            {
                FirstName    = FirstName,
                LastName     = LastName,
                Contacts     = Contacts,
                AccessFailed = AccessFailed,
                Cpf          = Cpf,
                IsActive     = IsActive,
                CreateOn     = CreateOn,
                UserId       = UserId,
                Roles        = RolesIds.Select(id => new Role {
                    RoleId = id
                }).ToList()
            };

            //user.Roles = db.Roles.Where(s => RolesIds.Any(id => id == s.RoleId)).AsNoTracking().ToList();
            return(user);
        }