本文整理汇总了C#中EFRepository.Add方法的典型用法代码示例。如果您正苦于以下问题:C# EFRepository.Add方法的具体用法?C# EFRepository.Add怎么用?C# EFRepository.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类EFRepository
的用法示例。
在下文中一共展示了EFRepository.Add方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddEmployee
public static bool AddEmployee(Employees NewEmployee)
{
EFRepository<Employees> EmployeesRepo = new EFRepository<Employees>();
EmployeesRepo.Add(NewEmployee);
GlobalUnitOfWork.Commit();
return true;
}
示例2: CreateNews_WithIncorrectData_ShouldThrow
public void CreateNews_WithIncorrectData_ShouldThrow()
{
// Arrange
this.CleanUpDatabase();
var listWithNews = new List<News>();
var repo = new EFRepository<News>(this.dbcontext);
var new1 = new News() { Content = null, Title = null, PublishedAt = DateTime.Now };
// Act
repo.Add(new1);
repo.SaveChanges();
}
示例3: Delete_Deletes_Record
public void Delete_Deletes_Record()
{
var newCustomer = new Customer
{
FirstName = ("John_DELETE_ME_" + DateTime.Now),
LastName = ("Doe_DELETE_ME_" + DateTime.Now),
StreetAddress1 = "This record was inserted for deletion",
City = "Fictional city",
State = "LA",
ZipCode = "12345"
};
//Re-usable query to query for the matching record.
var queryForCustomer = new Func<EFRepository<Customer>, Customer>
(
x => (from cust in x
where cust.FirstName == newCustomer.FirstName && cust.LastName == newCustomer.LastName
select cust).FirstOrDefault()
);
using (var scope = new UnitOfWorkScope())
{
var customerRepository = new EFRepository<Customer>();
var recordCheckResult = queryForCustomer(customerRepository);
Assert.That(recordCheckResult, Is.Null);
customerRepository.Add(newCustomer);
scope.Commit();
}
//Retrieve the record for deletion.
using (var scope = new UnitOfWorkScope())
{
var customerRepository = new EFRepository<Customer>();
var customerToDelete = queryForCustomer(customerRepository);
Assert.That(customerToDelete, Is.Not.Null);
customerRepository.Delete(customerToDelete);
scope.Commit();
}
//Ensure customer record is deleted.
using (new UnitOfWorkScope())
{
var customerRepository = new EFRepository<Customer>();
var recordCheckResult = queryForCustomer(customerRepository);
Assert.That(recordCheckResult, Is.Null);
}
}
示例4: Delete_ExistingNews_ShouldDeleteTheNew
public void Delete_ExistingNews_ShouldDeleteTheNew()
{
// Arrange
this.CleanUpDatabase();
var listWithNews = new List<News>();
var repo = new EFRepository<News>(this.dbcontext);
var new1 = new News() { Content = "Content of new", Title = "Title 1", PublishedAt = DateTime.Now };
// Act
repo.Add(new1);
repo.SaveChanges();
var newsInDb = repo.All().ToArray();
repo.Delete(newsInDb[0]);
repo.SaveChanges();
// Assert
Assert.AreEqual(0, repo.All().ToList().Count());
}
示例5: CreateNews_WithCorrectData_ShouldCreateNewsSuccessfully
public void CreateNews_WithCorrectData_ShouldCreateNewsSuccessfully()
{
// Arrange
this.CleanUpDatabase();
var listWithNews = new List<News>();
var repo = new EFRepository<News>(this.dbcontext);
var new1 = new News() { Content = "Content 1", Title = "Title 1", PublishedAt = DateTime.Now };
// Act
repo.Add(new1);
repo.SaveChanges();
var newsInDb = repo.All().ToArray();
// Assert
Assert.AreEqual(1, newsInDb.Count());
Assert.AreEqual(new1.Title, newsInDb[0].Title);
Assert.AreEqual(new1.Content, newsInDb[0].Content);
Assert.AreEqual(new1.PublishedAt.ToString(), newsInDb[0].PublishedAt.ToString());
}
示例6: Initialize
public void Initialize()
{
Context = new ParikshaContext();
EfUoW = new EFUnitOfWork(Context);
UserRepository = new EFRepository<UserDetail>(EfUoW, Context);
QuestionRepository = new EFRepository<Question>(EfUoW, Context);
StandardRepository = new EFRepository<Standard>(EfUoW, Context);
SubjectRepository = new EFRepository<Subject>(EfUoW, Context);
User = new UserDetail { UserRole = UserRole.Admin, Password = "Pwd", Name = "ashutosh", DateOfCreation = DateTime.UtcNow };
Standard = new Standard { StandardName = "First" };
Subject = new Subject { SubjectName = "Mathematics", SubjectCategory = "Algebra", Standard = Standard };
UserRepository.Add(User);
StandardRepository.Add(Standard);
SubjectRepository.Add(Subject);
EfUoW.Commit();
}
示例7: Save_New_Customer_Saves_Customer_When_UnitOfWork_Is_Committed
public void Save_New_Customer_Saves_Customer_When_UnitOfWork_Is_Committed()
{
var rnd = new Random();
var newCustomer = new Customer
{
FirstName = ("John_" + rnd.Next(0, 30000)),
LastName = ("Doe_" + rnd.Next(0, 30000)),
StreetAddress1 = "This record was inserted via a test",
City = "Fictional city",
State = "LA",
ZipCode = "12345"
};
var queryForCustomer = new Func<EFRepository<Customer>, Customer>
(
x => (from cust in x
where cust.FirstName == newCustomer.FirstName && cust.LastName == newCustomer.LastName
select cust).FirstOrDefault()
);
using (var scope = new UnitOfWorkScope())
{
var customerRepository = new EFRepository<Customer>();
var recordCheckResult = queryForCustomer(customerRepository);
Assert.That(recordCheckResult, Is.Null);
customerRepository.Add(newCustomer);
scope.Commit();
}
//Starting a completely new unit of work and repository to check for existance.
using (var scope = new UnitOfWorkScope())
{
var customerRepository = new EFRepository<Customer>();
var recordCheckResult = queryForCustomer(customerRepository);
Assert.That(recordCheckResult, Is.Not.Null);
Assert.That(recordCheckResult.FirstName, Is.EqualTo(newCustomer.FirstName));
Assert.That(recordCheckResult.LastName, Is.EqualTo(newCustomer.LastName));
customerRepository.Delete(recordCheckResult); //Deleting record after verification.
scope.Commit();
}
}
示例8: Save_Does_Not_Save_New_Customer_When_UnitOfWork_Is_Aborted
public void Save_Does_Not_Save_New_Customer_When_UnitOfWork_Is_Aborted()
{
var rnd = new Random();
var newCustomer = new Customer
{
FirstName = ("John_" + rnd.Next(30001, 50000)),
LastName = ("Doe_" + rnd.Next(30001, 50000)),
StreetAddress1 = "This record was inserted via a test",
City = "Fictional city",
State = "LA",
ZipCode = "12345"
};
using (new UnitOfWorkScope())
{
var customerRepository = new EFRepository<Customer>();
var recordCheckResult = (from cust in customerRepository
where cust.FirstName == newCustomer.FirstName &&
cust.LastName == newCustomer.LastName
select cust).FirstOrDefault();
Assert.That(recordCheckResult, Is.Null);
customerRepository.Add(newCustomer);
//DO NOT CALL COMMIT TO SIMMULATE A ROLLBACK.
}
//Starting a completely new unit of work and repository to check for existance.
using (var scope = new UnitOfWorkScope())
{
var customerRepository = new EFRepository<Customer>();
var recordCheckResult = (from cust in customerRepository
where cust.FirstName == newCustomer.FirstName &&
cust.LastName == newCustomer.LastName
select cust).FirstOrDefault();
Assert.That(recordCheckResult, Is.Null);
scope.Commit();
}
}
示例9: UpdateNews_WithIncorrectData_ShouldThrow
public void UpdateNews_WithIncorrectData_ShouldThrow()
{
// Arrange
this.CleanUpDatabase();
var listWithNews = new List<News>();
var repo = new EFRepository<News>(this.dbcontext);
var new1 = new News() { Content = "Content of new", Title = "Title 1", PublishedAt = DateTime.Now };
// Act
repo.Add(new1);
repo.SaveChanges();
var newsInDb = repo.All().ToArray();
string newTitle = null;
newsInDb[0].Title = newTitle;
repo.Update(newsInDb[0]);
repo.SaveChanges();
var latestNewsInDb = repo.All().ToArray();
// Assert
Assert.AreEqual(1, newsInDb.Count());
Assert.AreEqual(new1.Title, latestNewsInDb[0].Title);
Assert.AreEqual(new1.Content, latestNewsInDb[0].Content);
}
示例10: GetAllNews_ShouldReturnAllNews
public void GetAllNews_ShouldReturnAllNews()
{
// Arange
this.CleanUpDatabase();
var listWithNews = new List<News>();
var repo = new EFRepository<News>(this.dbcontext);
var new1 = new News() { Content = "Content 1", Title = "Title 1", PublishedAt = DateTime.Now };
var new2 = new News() { Content = "Content 2", Title = "Title 2", PublishedAt = DateTime.Now };
// Act
repo.Add(new1);
repo.Add(new2);
repo.SaveChanges();
listWithNews.Add(new1);
listWithNews.Add(new2);
// Assert
Assert.AreEqual(2, repo.All().Count());
CollectionAssert.AreEquivalent(listWithNews, repo.All().ToList());
}
示例11: Can_save
public void Can_save()
{
var customer = new Customer
{
FirstName = "Jane",
LastName = "Doe",
StreetAddress1 = "123 Main St",
City = "Sunset City",
State = "LA",
ZipCode = "12345"
};
using (var scope = new UnitOfWorkScope())
{
var repository = new EFRepository<Customer>();
repository.Add(customer);
scope.Commit();
}
Assert.That(customer.CustomerID, Is.GreaterThan(0));
using (var testData = new EFTestData(OrdersContextProvider()))
{
Customer savedCustomer = null;
testData.Batch(action => savedCustomer = action.GetCustomerById(customer.CustomerID));
Assert.That(savedCustomer, Is.Not.Null);
Assert.That(savedCustomer.CustomerID, Is.EqualTo(customer.CustomerID));
}
}