本文整理汇总了C#中GenericRepository.SaveChanges方法的典型用法代码示例。如果您正苦于以下问题:C# GenericRepository.SaveChanges方法的具体用法?C# GenericRepository.SaveChanges怎么用?C# GenericRepository.SaveChanges使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GenericRepository
的用法示例。
在下文中一共展示了GenericRepository.SaveChanges方法的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();
}
示例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();
}
示例3: 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();
}
示例4: 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
}
示例5: 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();
}
示例6: 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();
}
示例7: 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);
}
示例8: 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);
}
示例9: ExportCountriesFromMSSqlToMySql
private static void ExportCountriesFromMSSqlToMySql(GenericRepository<Country> sqlCountriesRepo, GenericRepository<Country> mySqlCountriesRepo)
{
var countriesFromMSsql = sqlCountriesRepo.GetAll().ToList();
foreach (var countryFromMSsql in countriesFromMSsql.ToList())
{
var country = new Country()
{
Name = countryFromMSsql.Name
};
mySqlCountriesRepo.Add(country);
mySqlCountriesRepo.SaveChanges();
}
}
示例10: ExportCitiesFromMSSqlToMySQL
private static void ExportCitiesFromMSSqlToMySQL(GenericRepository<City> sqlCitiesRepo, GenericRepository<City> mySqlCitiesRepo)
{
var mysqlCountries = new GenericRepository<Country>(new GirlAgencyMySql.Data.MySql());
var citiesFromMSsql = sqlCitiesRepo.GetAll().ToList();
foreach (var cityFromMSsql in citiesFromMSsql.ToList())
{
var city = new City()
{
Name = cityFromMSsql.Name,
CountryId = mysqlCountries.
GetAll()
.FirstOrDefault(n => n.CountryId == cityFromMSsql.CountryId).CountryId
};
mySqlCitiesRepo.Add(city);
mySqlCitiesRepo.SaveChanges();
}
}
示例11: UpdateNews_Should_Successfully_Update_News
public void UpdateNews_Should_Successfully_Update_News()
{
var repository = new GenericRepository<News>(new NewsContext());
var newsToUpdate = repository.All().FirstOrDefault();
if (newsToUpdate == null)
{
Assert.Fail("Cannot run test - no news in the repository");
}
var newsModel = new NewsBindingModel
{
Title = "Updated Sample title.",
Content = "Updated Sample content",
PublishDate = DateTime.Now
};
newsToUpdate.Title = newsModel.Title;
newsToUpdate.Content = newsModel.Content;
if (newsModel.PublishDate.HasValue)
{
newsToUpdate.PublishDate = newsModel.PublishDate.Value;
}
repository.Update(newsToUpdate);
repository.SaveChanges();
}
示例12: ModifyMovieWhenExistingItemShouldModify
public void ModifyMovieWhenExistingItemShouldModify()
{
//Arrange -> prepare the objects
var repo = new GenericRepository<Movie>(new MoviesGalleryContext());
var movie = new Movie()
{
Id = 1,
Length = 10,
Ration = 10
};
//Act -> perform some logic
repo.Add(movie);
repo.SaveChanges();
movie.Length = 2;
movie.Ration = 7;
repo.Update(movie);
repo.SaveChanges();
var movieFromDb = repo.GetById(movie.Id);
Assert.IsNotNull(movieFromDb);
Assert.AreEqual(2, movieFromDb.Length);
Assert.AreEqual(7, movieFromDb.Ration);
Assert.IsTrue(movieFromDb.Id != 0);
}
示例13: ListAllMovieShouldReturnAllMovies
public void ListAllMovieShouldReturnAllMovies()
{
var repo = new GenericRepository<Movie>(new MoviesGalleryContext());
var fakeMovies = new List<Movie>()
{
new Movie()
{
Id = 1,
Length = 1,
Ration = 1
},
new Movie()
{
Id = 2,
Length = 2,
Ration = 2
},
new Movie()
{
Id = 3,
Length = 3,
Ration = 3
},
};
foreach (var fakeMovie in fakeMovies)
{
repo.Add(fakeMovie);
repo.SaveChanges();
}
var fakeMovieLengths = fakeMovies.Select(m => m.Length).ToList();
var movieDbLengths = repo.All().Select(m => m.Length).ToList();
CollectionAssert.AreEqual(fakeMovieLengths, movieDbLengths);
}
示例14: ImportOrdersToDatabase
private static void ImportOrdersToDatabase(IEnumerable<Order> orders)
{
var sqlRepo = new GenericRepository<Order>(new GirlsAgencyContext());
var girlRepo = new GenericRepository<Girl>(new GirlsAgencyContext());
var ctx = new GirlsAgencyContext();
foreach (var order in orders)
{
sqlRepo.Add(order);
//ctx.Customers.
}
sqlRepo.SaveChanges();
}
示例15: ExportGirlsFromMSSqlToMySql
private static void ExportGirlsFromMSSqlToMySql(GenericRepository<Girl> sqlGirlsRepo, GenericRepository<Girl> mySqlGirlsRepo)
{
var girlsFromMSsql = sqlGirlsRepo.GetAll().ToList();
foreach (var girlFromMSsql in girlsFromMSsql.ToList())
{
var girl = new Girl()
{
FirstName = girlFromMSsql.FirstName,
LastName = girlFromMSsql.LastName,
Age = girlFromMSsql.Age,
BreastSizeId = girlFromMSsql.BreastSizeId,
HairColorId = girlFromMSsql.HairColorId,
CityId = girlFromMSsql.CityId,
PricePerHour = girlFromMSsql.PricePerHour,
Features = girlFromMSsql.Features,
CountyId = girlFromMSsql.CountyId,
// City = girlFromMSsql.City,
// Country = girlFromMSsql.Country
};
mySqlGirlsRepo.Add(girl);
mySqlGirlsRepo.SaveChanges();
}
}