示例#1
0
        public void Seed()
        {
            // SchoolType seeder
            var schoolTypes = new DbEntities.Office.SchoolType[]
            {
                new DbEntities.Office.SchoolType {
                    Name = "Graduate"
                },
                new DbEntities.Office.SchoolType {
                    Name = "Undergraduate"
                },
                new DbEntities.Office.SchoolType {
                    Name = "Higher Secondary"
                },
                new DbEntities.Office.SchoolType {
                    Name = "Secondary"
                },
            };

            _context.SchoolType.AddOrUpdate(
                p => p.Name,
                schoolTypes
                );
            _context.SaveChanges();

            // School Seeder
            var underGradSchoolType = _context.SchoolType.FirstOrDefault(x => x.Name == "Undergraduate");
            var schools             = new DbEntities.Office.School[]
            {
                new DbEntities.Office.School {
                    Name            = "Nepal Engineering College",
                    Code            = SchoolSeeder.NEC_CODE,
                    Address         = "Changunarayan, Bhaktapur, Nepal",
                    IsActive        = true,
                    IsDeleted       = false,
                    PhoneAfterHours = "",
                    PhoneMain       = "",
                    SchoolTypeId    = underGradSchoolType.Id,
                    UserId          = null,
                    Website         = "http://nec.edu.np",
                    CreatedDate     = DateTime.Now,
                    Description     = "",
                    EmailGeneral    = "",
                    EmailMarketing  = "",
                    EmailSupport    = "",
                    ImageId         = 0
                }
            };

            _context.School.AddOrUpdate(
                p => p.Name,
                schools
                );
            _context.SaveChanges();
        }
示例#2
0
            public DbEntities.Office.School AddOrUpdateSchool(DbEntities.Office.School school, UserFile image)//System.Web.HttpPostedFile httpPostedFile
            {
                try
                {
                    using (var scope = new TransactionScope())
                    {
                        int earlierSchoolId = school.Id;
                        var ent             = Context.School.Find(school.Id);
                        if (ent == null)
                        {
                            var img = Context.File.Add(image);
                            Context.SaveChanges();
                            school.ImageId = img.Id;
                            ent            = Context.School.Add(school);
                            Context.SaveChanges();

                            //if this is newly created school (by the user) then add schoolId to the user
                            if (earlierSchoolId <= 0 && school.UserId > 0)
                            {
                                var user = Context.Users.Find(school.UserId);
                                if (user != null)
                                {
                                    user.SchoolId = ent.Id;
                                    Context.SaveChanges();
                                }
                            }
                        }
                        else
                        {
                            //update later
                            if (ent.ImageId <= 0)
                            {
                                //var cntx = new AcademicContext();
                                var img = Context.File.Add(image);
                                Context.SaveChanges();
                                school.ImageId = img.Id;
                                //school.ImageId = img.Id;

                                //ent.ImageId = img.Id;
                                //Context.Dispose();
                            }
                            else
                            {
                                var img = Context.File.Find(ent.ImageId);
                                if (img != null)
                                {
                                    img.DisplayName   = image.DisplayName;
                                    img.FileName      = image.FileName;
                                    img.ModifiedBy    = image.ModifiedBy;
                                    img.ModifiedDate  = image.ModifiedDate;
                                    img.FileDirectory = image.FileDirectory;

                                    img.FileSizeInBytes = image.FileSizeInBytes;
                                    img.FileType        = image.FileType;
                                    img.IconPath        = image.IconPath;
                                    img.Void            = image.Void;
                                    //Context.SaveChanges();
                                }
                            }

                            ent.ImageId         = school.ImageId;
                            ent.Name            = school.Name;
                            ent.Description     = school.Description;
                            ent.EmailGeneral    = school.EmailGeneral;
                            ent.EmailMarketing  = school.EmailMarketing;
                            ent.EmailSupport    = school.EmailSupport;
                            ent.PhoneMain       = school.PhoneMain;
                            ent.PhoneAfterHours = school.PhoneAfterHours;
                            ent.Address         = school.Address;
                            ent.Website         = school.Website;
                            ent.Code            = school.Code;

                            //ent.Image = school.Image;
                            //ent.ImageType = school.ImageType;

                            ent.IsActive  = school.IsActive;
                            ent.IsDeleted = school.IsDeleted;
                            Context.SaveChanges();
                        }
                        scope.Complete();
                        return(ent);
                    }
                }
                catch (DbEntityValidationException ex)
                {
                    throw ex;
                }
                catch (Exception)
                {
                    return(null);
                }
            }