当前位置: 首页>>代码示例>>C#>>正文


C# GenericRepository.Add方法代码示例

本文整理汇总了C#中GenericRepository.Add方法的典型用法代码示例。如果您正苦于以下问题:C# GenericRepository.Add方法的具体用法?C# GenericRepository.Add怎么用?C# GenericRepository.Add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在GenericRepository的用法示例。


在下文中一共展示了GenericRepository.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Post

        public IHttpActionResult Post(HomeworkRequestModel model)
        {
            if (model == null)
            {
                return this.BadRequest();
            }

            var db = new StudentSystemDbContext();
            var homeworsRepo = new GenericRepository<Homework>(db);
            var coursesRepo = new GenericRepository<Course>(db);
            var studentsRepo = new GenericRepository<Student>(db);
            Course defaultCourse = coursesRepo.All().ToList().FirstOrDefault();
            Student defaultStudent = studentsRepo.All().ToList().FirstOrDefault();
            var homeworkToAdd = new Homework
            {
                FileUrl = model.FileUrl,
                TimeSent = new DateTime(model.TimeSentTicks),
                Course = defaultCourse,
                CourseId = defaultCourse.Id,
                Student = defaultStudent,
                StudentIdentification = defaultStudent.StudentIdentification
            };

            homeworsRepo.Add(homeworkToAdd);
            homeworsRepo.SaveChanges();
            return this.Ok();
        }
开发者ID:vassildinev,项目名称:Web-Services,代码行数:27,代码来源:HomeworksController.cs

示例2: Post

        public IHttpActionResult Post(TestRequestModel model)
        {
            if (model == null)
            {
                return this.BadRequest();
            }

            var db = new StudentSystemDbContext();
            var testsRepo = new GenericRepository<Test>(db);
            var coursesRepo = new GenericRepository<Course>(db);
            List<Course> allCourses = coursesRepo.All().ToList();
            var testCourse = new Course
            {
                Name = model.Course.Name,
                Description = model.Course.Description
            };

            var testToAdd = new Test
            {
                Course = testCourse
            };

            testsRepo.Add(testToAdd);
            testsRepo.SaveChanges();
            return this.Ok();
        }
开发者ID:vassildinev,项目名称:Web-Services,代码行数:26,代码来源:TestsController.cs

示例3: Add

        public object Add(AddTripModel model)
        {
            GenericRepository<Trip> TripRepo = new GenericRepository<Trip>();
            GenericRepository<User> userRepo = new GenericRepository<User>();
             User user = userRepo.GetSingle(u => u.Username == User.Identity.Name);
            DateTime departDate;
            if (!DateTime.TryParse(model.Depdate, out departDate))
            {
                return false;
            }

            Trip Trip = new Trip()
            {
                UserId =user.UserId,
                DepartDate = departDate,
                AvailableSeatNumber = model.Seatnum,
                Price = model.Price,
                DepMin=model.Min,
                DepHour=model.Hour,
                EstimatedHour=model.EstHour,
                EstimatedMin=model.EstMin,
                DepartLocationId=model.DepLocId,
                ArrivalLocationId=model.ArrLocId,
                CarId = 1,

            };
            TripRepo.Add(Trip);
            return true;
        }
开发者ID:yigityesilpinar,项目名称:Web-Based-Application-of-Carpooling,代码行数:29,代码来源:TripController.cs

示例4: Can_Add_New_Entity

        public void Can_Add_New_Entity()
        {
            // Arrange
            var routesRepository = new GenericRepository<Route>(this.dataBaseContext);

            // Act
            routesRepository.Add(new Route { Id = 4, Name = "newRoute" });

            // Assert
            Assert.AreEqual(2, routesRepository.Count);
            Assert.AreNotEqual(null, routesRepository.GetById(1));
        }
开发者ID:sashaalex67,项目名称:CityTravel,代码行数:12,代码来源:GenericRepositoryTests.cs

示例5: AddNews_Should_Throw_Exception_If_Data_Is_Invalid

        public void AddNews_Should_Throw_Exception_If_Data_Is_Invalid()
        {
            var repository = new GenericRepository<News>(new NewsContext());
            var news = new News
            {
                Title = "shor",
                Content = "Sample Content",
                PublishDate = DateTime.Now
            };

            repository.Add(news);
            repository.SaveChanges();
        }
开发者ID:OmniPot,项目名称:SoftUni-Level-3-Backend,代码行数:13,代码来源:RepositoryTests.cs

示例6: TransferLocations

        public void TransferLocations()
        {
            GenericRepository<Location> locationRepo = new GenericRepository<Location>();
            string file = System.Web.HttpContext.Current.Server.MapPath("~/files/provinces.csv");
            using (StreamReader reader = new StreamReader(file, Encoding.UTF8))
            {
                string line = string.Empty;
                while ((line = reader.ReadLine()) != null)
                {
                    string[] components = line.Split(',');
                    Location location = new Location()
                    {
                        Name = components[2],
                        Lat = Convert.ToDouble(components[3]),
                        Lon = Convert.ToDouble(components[4]),
                        ParentId = -1
                    };
                    locationRepo.Add(location);
                }
            }

            file = System.Web.HttpContext.Current.Server.MapPath("~/files/districts.csv");
            using (StreamReader reader = new StreamReader(file, Encoding.UTF8))
            {
                string line = string.Empty;
                while ((line = reader.ReadLine()) != null)
                {
                    string[] components = line.Split(',');
                    Location location = new Location()
                    {
                        Name = components[2],
                        Lat = Convert.ToDouble(components[3]),
                        Lon = Convert.ToDouble(components[4]),
                        ParentId = Convert.ToInt32(components[1])
                    };
                    locationRepo.Add(location);
                }
            }
        }
开发者ID:yigityesilpinar,项目名称:Web-Based-Application-of-Carpooling,代码行数:39,代码来源:MaintenanceController.cs

示例7: AddMovieWhenMovieIsInvalidShouldThrowException

        public void AddMovieWhenMovieIsInvalidShouldThrowException()
        {
            //Arrange -> prepare the objects
            var repo = new GenericRepository<Movie>(new MoviesGalleryContext());
            var invalidMovie = new Movie()
            {
                Id = 1,
                Length = 20,
                Ration = 20
            };

            //Act -> perform some logic
            repo.Add(invalidMovie);
            repo.SaveChanges();

            //Asssert -> expect and exception
        }
开发者ID:Aleksandyr,项目名称:Software-University,代码行数:17,代码来源:RepositoeisTest.cs

示例8: Post

        // POST api/Albums
        public IHttpActionResult Post([FromBody]AlbumServiceModel value)
        {
            var artists = new GenericRepository<Artist>(Data);
            var songs = new GenericRepository<Song>(Data);

            if (value.Artists != null)
            {
                foreach (var artist in value.Artists)
                {
                    if (!artists.All().Any(x => x.Name == artist))
                    {
                        var newArtist = new Artist { Name = artist };
                        artists.Add(newArtist);
                    }

                    Data.SaveChanges();
                }
            }

            if (value.Songs != null)
            {
                foreach (var song in value.Songs)
                {
                    if (!songs.All().Any(x => x.Title == song))
                    {
                        var newSong = new Song { Title = song };
                        songs.Add(newSong);
                    }

                    Data.SaveChanges();
                }
            }

            var album = new Album
            {
                Title = value.Title,
                Genre = value.Genre,
                ReleasedOn = value.ReleasedOn,
                Artists = artists.All().Where(x => value.Artists.Contains(x.Name)).ToList(),
                Songs = songs.All().Where(x => value.Songs.Contains(x.Title)).ToList()
            };

            AlbumsRepository.Add(album);
            Data.SaveChanges();
            return this.StatusCode(HttpStatusCode.Created);
        }
开发者ID:b-slavov,项目名称:Telerik-Software-Academy,代码行数:47,代码来源:AlbumsController.cs

示例9: Post

        public IHttpActionResult Post(CourseRequestModel model)
        {
            if (model == null)
            {
                return this.BadRequest();
            }

            var db = new StudentSystemDbContext();
            var coursesRepo = new GenericRepository<Course>(db);
            var courseToAdd = new Course
            {
                Name = model.Name,
                Description = model.Description
            };

            coursesRepo.Add(courseToAdd);
            coursesRepo.SaveChanges();
            return this.Ok();
        }
开发者ID:vassildinev,项目名称:Web-Services,代码行数:19,代码来源:CoursesController.cs

示例10: DoUpsert

        public ResponseMessageModel DoUpsert(SignageDigitalEntities db, Media model)
        {
            var repository = new GenericRepository<Media>(db);

            if (model.MediaId == EntityConstants.NULL_VALUE)
            {
                repository.Add(model);
            }
            else
            {
                repository.Update(model);
            }

            return new ResponseMessageModel
            {
                HasError = false,
                Title = ResShared.TITLE_REGISTER_SUCCESS,
                Message = ResShared.INFO_REGISTER_SAVED
            };
        }
开发者ID:stphosvsol,项目名称:SignageDigitalPortal,代码行数:20,代码来源:MediaService.cs

示例11: Add_NULL_Object_Must_Not_Add

        public void Add_NULL_Object_Must_Not_Add()
        {
            //
            // Arrange
            //
            var mockDbSet = new Mock<DbSet<IModel>>();
            var mockContext = new Mock<EFDbContext>();

            mockContext.Setup(x => x.Set<IModel>()).Returns(mockDbSet.Object);

            var repository = new GenericRepository<IModel>(mockContext.Object);
            //
            // Act
            //
            repository.Add(null);
            //
            // Assert
            //
            mockDbSet.Verify(x => x.Add(It.IsAny<IModel>()), Times.Never);
        }
开发者ID:Cheranga,项目名称:DAL,代码行数:20,代码来源:GenericRepositoryTest.cs

示例12: Post

        public IHttpActionResult Post(StudentRequestModel model)
        {
            if (model == null)
            {
                return this.BadRequest();
            }

            var db = new StudentSystemDbContext();
            var studentsRepo = new GenericRepository<Student>(db);
            var studentToAdd = new Student
            {
                FirstName = model.FirstName,
                LastName = model.LastName,
                Level = model.Level
            };

            studentsRepo.Add(studentToAdd);
            studentsRepo.SaveChanges();
            return this.Ok();
        }
开发者ID:vassildinev,项目名称:Web-Services,代码行数:20,代码来源:StudentsController.cs

示例13: AddNews_Should_Successfully_Add_Specified_News_To_The_Repository

        public void AddNews_Should_Successfully_Add_Specified_News_To_The_Repository()
        {
            var repository = new GenericRepository<News>(new NewsContext());
            var news = new News
            {
                Title = "Sample Title",
                Content = "Sample Content",
                PublishDate = DateTime.Now
            };

            repository.Add(news);
            repository.SaveChanges();

            var newsFromDb = repository.Find(news.Id);

            Assert.IsNotNull(newsFromDb);
            Assert.AreEqual(news.Content, newsFromDb.Content);
            Assert.AreEqual(news.Title, newsFromDb.Title);
            Assert.AreEqual(news.PublishDate, newsFromDb.PublishDate);
            Assert.IsTrue(newsFromDb.Id != 0);
        }
开发者ID:OmniPot,项目名称:SoftUni-Level-3-Backend,代码行数:21,代码来源:RepositoryTests.cs

示例14: AddMovieWhenMovieIsAddedToDbShouldReturnMovie

        public void AddMovieWhenMovieIsAddedToDbShouldReturnMovie()
        {
            //Arange -> prepare the objects
            var repo = new GenericRepository<Movie>(new MoviesGalleryContext());
            var movie = new Movie()
            {
                Id = 1,
                Length = 1,
                Ration = 3
            };

            //Act -> perform some logic
            repo.Add(movie);
            repo.SaveChanges();

            //Assert -> validate the results
            var movieFromDb = repo.GetById(movie.Id);

            Assert.IsNotNull(movieFromDb);
            Assert.AreEqual(movie.Length, movieFromDb.Length);
            Assert.AreEqual(movie.Ration, movieFromDb.Ration);
            Assert.IsTrue(movieFromDb.Id != 0);
        }
开发者ID:Aleksandyr,项目名称:Software-University,代码行数:23,代码来源:RepositoeisTest.cs

示例15: Add_Valid_Object_Must_Add

        public void Add_Valid_Object_Must_Add()
        {
            //
            // Arrange
            //
            var mockModel = new Mock<IModel>();
            mockModel.Setup(x => x.Id).Returns(1);

            var mockDbSet = new Mock<DbSet<IModel>>();
            var mockContext = new Mock<EFDbContext>();

            mockContext.Setup(x => x.Set<IModel>()).Returns(mockDbSet.Object);

            var repository = new GenericRepository<IModel>(mockContext.Object);
            //
            // Act
            //
            repository.Add(mockModel.Object);
            //
            // Assert
            //
            mockDbSet.Verify(x => x.Add(It.IsAny<IModel>()), Times.Once);
        }
开发者ID:Cheranga,项目名称:DAL,代码行数:23,代码来源:GenericRepositoryTest.cs


注:本文中的GenericRepository.Add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。