本文整理汇总了C#中BrightstarDB.Tests.EntityFramework.MyEntityContext.SaveChanges方法的典型用法代码示例。如果您正苦于以下问题:C# MyEntityContext.SaveChanges方法的具体用法?C# MyEntityContext.SaveChanges怎么用?C# MyEntityContext.SaveChanges使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BrightstarDB.Tests.EntityFramework.MyEntityContext
的用法示例。
在下文中一共展示了MyEntityContext.SaveChanges方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestLinqCount
public void TestLinqCount()
{
var connectionString = GetConnectionString("TestLinqCount");
var context = new MyEntityContext(connectionString);
for(var i = 0; i<100; i++)
{
var entity = context.Entities.Create();
entity.SomeString = "Entity " + i;
entity.SomeInt = i;
}
context.SaveChanges();
var count = context.Entities.Count();
Assert.IsNotNull(count);
Assert.AreEqual(100, count);
for (var j = 0; j < 100; j++)
{
var entity = context.Entities.Create();
entity.SomeString = "Entity " + j;
entity.SomeInt = j;
}
context.SaveChanges();
var count2 = context.Entities.Count();
Assert.IsNotNull(count2);
Assert.AreEqual(200, count2);
}
示例2: TestSavingCallbackCalled
public void TestSavingCallbackCalled()
{
var context = new MyEntityContext(_connectionString);
_changedItems.Clear();
context.SavingChanges += LogChangedItems;
var alice = new Person {Name = "Alice"};
context.Persons.Add(alice);
var bob = context.Persons.Create();
bob.Name = "Bob";
context.SaveChanges();
Assert.AreEqual(2, _changedItems.Count);
Assert.IsTrue(_changedItems.Cast<Person>().Any(p=>p.Id.Equals(alice.Id)));
Assert.IsTrue(_changedItems.Cast<Person>().Any(p=>p.Id.Equals(bob.Id)));
_changedItems.Clear();
bob.Friends.Add(alice);
context.SaveChanges();
Assert.AreEqual(1, _changedItems.Count);
Assert.IsTrue(_changedItems.Cast<Person>().Any(p => p.Id.Equals(bob.Id)));
_changedItems.Clear();
var skill = new Skill {Name = "Programming"};
context.Skills.Add(skill);
context.SaveChanges();
_changedItems.Clear();
skill.SkilledPeople.Add(bob);
context.SaveChanges();
Assert.AreEqual(1, _changedItems.Count);
Assert.IsTrue(_changedItems.Cast<Person>().Any(p => p.Id.Equals(bob.Id)));
_changedItems.Clear();
}
示例3: TestAddToInverse
public void TestAddToInverse()
{
string productionId, performanceId;
using (var context = new MyEntityContext(_connectionString))
{
var production = context.Productions.Create();
var performance = context.Performances.Create();
Assert.That(production.Performances.Count, Is.EqualTo(0));
Assert.That(production.Photos.Count, Is.EqualTo(0));
Assert.That(production.ProductionTeam.Count, Is.EqualTo(0));
// Add the performance to the production's perfomances collection
production.Performances.Add(performance);
Assert.That(production.Performances.Count, Is.EqualTo(1));
Assert.That(production.Photos.Count, Is.EqualTo(0));
Assert.That(production.ProductionTeam.Count, Is.EqualTo(0));
context.SaveChanges();
productionId = production.Id;
performanceId = performance.Id;
}
using (var context = new MyEntityContext(_connectionString))
{
var production = context.Productions.FirstOrDefault(x => x.Id.Equals(productionId));
Assert.That(production, Is.Not.Null);
Assert.That(production.Performances.Count, Is.EqualTo(1));
Assert.That(production.Performances.First().Id, Is.EqualTo(performanceId));
Assert.That(production.Photos.Count, Is.EqualTo(0));
Assert.That(production.ProductionTeam.Count, Is.EqualTo(0));
}
}
示例4: TestCreatePlainLiteral
public void TestCreatePlainLiteral()
{
var storeName = "PlainLiteralTests_CreatePlainLiteral" + DateTime.Now.Ticks;
using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
{
string conceptAId;
using (var context = new MyEntityContext(dataObjectStore))
{
var conceptA = context.Concepts.Create();
conceptA.PrefLabel = new []
{
new PlainLiteral("Default value"),
new PlainLiteral("English value", "en"),
new PlainLiteral("US English value", "en-US"),
};
context.SaveChanges();
conceptAId = conceptA.Id;
}
using (var context = new MyEntityContext(dataObjectStore))
{
var conceptA = context.Concepts.FirstOrDefault(c => c.Id.Equals(conceptAId));
Assert.IsNotNull(conceptA);
Assert.That(conceptA.PrefLabel.Count, Is.EqualTo(3));
Assert.That(conceptA.PrefLabel.Any(l=>l.Value.Equals("Default value") && l.Language.Equals(String.Empty)));
Assert.That(conceptA.PrefLabel.Any(l=>l.Value.Equals("English value") && l.Language.Equals("en")));
Assert.That(conceptA.PrefLabel.Any(l=>l.Value.Equals("US English value") && l.Language.Equals("en-us")));
}
}
}
示例5: TestInsertIntoDefaultGraph
public void TestInsertIntoDefaultGraph()
{
var storeName = "http://www.brightstardb.com/tests#empty";
var connectionString = MakeStoreConnectionString(storeName);
var dataObjectContext = BrightstarService.GetDataObjectContext(connectionString);
string aliceId;
using (var store = dataObjectContext.OpenStore(storeName))
{
using (var context = new MyEntityContext(store))
{
var alice = context.FoafPersons.Create();
aliceId = alice.Id;
context.SaveChanges();
}
}
using (var store = dataObjectContext.OpenStore(storeName))
{
using (var context = new MyEntityContext(store))
{
var alice = context.FoafPersons.FirstOrDefault(p => p.Id.Equals(aliceId));
Assert.That(alice, Is.Not.Null);
}
}
}
示例6: QueryableCollectionsTests
public QueryableCollectionsTests()
{
var connectionString = ConnectionString + "StoreName=SimpleCollectionFilter_" + DateTime.UtcNow.Ticks;
_context = new MyEntityContext(connectionString);
var gardening = _context.Skills.Create();
gardening.Name = "Gardening";
var painting = _context.Skills.Create();
painting.Name = "Painting";
var carpentry = _context.Skills.Create();
carpentry.Name = "Carpentry";
_dept = _context.Departments.Create();
_dept.Name = "Department99";
var andy = _context.Persons.Create();
andy.Name = "Andy";
andy.Skills.Add(gardening);
andy.Skills.Add(painting);
andy.Skills.Add(carpentry);
var arnold = _context.Persons.Create();
arnold.Name = "Arnold";
arnold.Skills.Add(painting);
var bert = _context.Persons.Create();
bert.Name = "Bert";
_dept.Persons.Add(andy);
_dept.Persons.Add(bert);
_context.SaveChanges();
}
示例7: TestUseDerivedInstanceInBaseClassCollectionProperty
public void TestUseDerivedInstanceInBaseClassCollectionProperty()
{
var storeName = MakeStoreName("useDerivedInstance");
var context = new MyEntityContext(ConnectionString + storeName);
var entity1 = context.DerivedEntities.Create();
entity1.BaseStringValue = "Entity1";
var entity2 = context.DerivedEntities.Create();
entity2.BaseStringValue = "Entity2";
var entity3 = context.BaseEntities.Create();
entity3.BaseStringValue = "Entity3";
entity1.RelatedEntities.Add(entity2);
entity1.RelatedEntities.Add(entity3);
context.SaveChanges();
context=new MyEntityContext(ConnectionString + storeName);
var baseEntities = context.BaseEntities.ToList();
Assert.AreEqual(3, baseEntities.Count);
var derivedEntities = context.DerivedEntities.ToList();
Assert.AreEqual(2, derivedEntities.Count);
entity1 = context.DerivedEntities.Where(x => x.BaseStringValue.Equals("Entity1")).FirstOrDefault();
Assert.IsNotNull(entity1);
Assert.AreEqual(2, entity1.RelatedEntities.Count);
Assert.IsTrue(entity1.RelatedEntities.Any(x=>x.BaseStringValue.Equals("Entity2")));
Assert.IsTrue(entity1.RelatedEntities.Any(x=>x.BaseStringValue.Equals("Entity3")));
}
示例8: TestSetAndGetSimpleProperty
public void TestSetAndGetSimpleProperty()
{
string storeName = Guid.NewGuid().ToString();
var dataObjectStore = _dataObjectContext.CreateStore(storeName);
var context = new MyEntityContext(dataObjectStore);
var person = context.Persons.Create();
Assert.IsNotNull(person);
person.Name = "Kal";
context.SaveChanges();
var personId = person.Id;
// Test that the property is still there when we retrieve the object again
dataObjectStore = _dataObjectContext.OpenStore(storeName);
context = new MyEntityContext(dataObjectStore);
person = context.Persons.Where(p => p.Id == personId).FirstOrDefault();
Assert.IsNotNull(person);
Assert.IsNotNull(person.Name, "person.Name was NULL when retrieved back from server");
Assert.AreEqual("Kal", person.Name, "Unexpected Name property value");
// Test we can also use the simple property in a LINQ query
dataObjectStore = _dataObjectContext.OpenStore(storeName);
context = new MyEntityContext(dataObjectStore);
person = context.Persons.Where(p => p.Name == "Kal").FirstOrDefault();
Assert.IsNotNull(person, "Could not find person by Name");
Assert.AreEqual(personId, person.Id, "Query for person by name returned an unexpected person entity");
// Test we can use ToList()
var people = context.Persons.Where(p => p.Name == "Kal").ToList();
Assert.IsNotNull(people);
Assert.AreEqual(1, people.Count);
Assert.AreEqual(personId, people[0].Id);
}
示例9: TestSavingChangesUpdatesTimestamp
public void TestSavingChangesUpdatesTimestamp()
{
IArticle article;
DateTime saving, updating;
using (var context = new MyEntityContext(_connectionString))
{
context.SavingChanges += UpdateTrackable;
article = context.Articles.Create();
article.Title = "My Test Article";
saving = DateTime.Now;
context.SaveChanges();
}
using (var context = new MyEntityContext(_connectionString))
{
context.SavingChanges += UpdateTrackable;
article = context.Articles.FirstOrDefault(a => a.Id.Equals(article.Id));
Assert.IsNotNull(article);
Assert.IsTrue(article.Created >= saving);
Assert.IsTrue(article.LastModified >= saving);
article.BodyText = "Some body text";
updating = DateTime.Now;
context.SaveChanges();
}
using (var context = new MyEntityContext(_connectionString))
{
article = context.Articles.FirstOrDefault(a => a.Id.Equals(article.Id));
Assert.IsNotNull(article);
Assert.IsTrue(article.Created >= saving);
Assert.IsTrue(article.LastModified >= updating);
}
}
示例10: TestCreateAndRetrieve
public void TestCreateAndRetrieve()
{
string storeName = Guid.NewGuid().ToString();
string personId;
using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
{
using (var context = new MyEntityContext(dataObjectStore))
{
var person = context.Persons.Create();
Assert.IsNotNull(person);
context.SaveChanges();
Assert.IsNotNull(person.Id);
personId = person.Id;
}
}
using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
{
using (var context = new MyEntityContext(dataObjectStore))
{
var person = context.Persons.FirstOrDefault(p => p.Id == personId);
Assert.IsNotNull(person);
}
}
}
示例11: SetUp
public void SetUp()
{
_context = new MyEntityContext("type=embedded;storesDirectory=" + Configuration.StoreLocation + ";storeName=EFStringComparisonTests_" + DateTime.Now.Ticks);
var np = new Company {Name = "NetworkedPlanet"};
var apple = new Company {Name = "Apple"};
_context.Companies.Add(np);
_context.Companies.Add(apple);
_context.SaveChanges();
}
示例12: TestSaveWorksWhenNoCallback
public void TestSaveWorksWhenNoCallback()
{
var context = new MyEntityContext(_connectionString);
var carol = new Person { Name = "Carol" };
context.Persons.Add(carol);
context.SaveChanges();
var found = context.Persons.FirstOrDefault(p => p.Name.Equals("Carol"));
Assert.IsNotNull(found);
}
示例13: TestCannotCreateEntityWithKey
public void TestCannotCreateEntityWithKey()
{
string storeName = "CannotCreateEntityWithKey_" + DateTime.UtcNow.Ticks;
using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
{
using (var context = new MyEntityContext(dataObjectStore))
{
// Should throw an exception as the Name property is required to generate the key
context.StringKeyEntities.Create();
context.SaveChanges();
}
}
}
示例14: PropertyChangeNotificationTests
public PropertyChangeNotificationTests()
{
_storeName = "PropertyChangeNotificationTests_" + DateTime.UtcNow.Ticks;
_context = new MyEntityContext("type=embedded;storesDirectory=c:\\brightstar;storeName="+_storeName);
_ftse = _context.Markets.Create();
_nyse = _context.Markets.Create();
_company = _context.Companies.Create();
_company.Name = "Glaxo";
_company.HeadCount = 20000;
_company.PropertyChanged += HandlePropertyChanged;
_person = _context.FoafPersons.Create();
(_person.MboxSums as INotifyCollectionChanged).CollectionChanged += HandleCollectionChanged;
_context.SaveChanges();
}
示例15: TestLinqLongCount
public void TestLinqLongCount()
{
var connectionString = _connectionString + "StoreName=" +Guid.NewGuid();
var context = new MyEntityContext(connectionString);
for (var i = 0; i < 10000; i++)
{
var entity = context.Entities.Create();
entity.SomeString = "Entity " + i;
if (i % 2000 == 0)
{
context.SaveChanges();
}
}
context.SaveChanges();
var context2 = new MyEntityContext(connectionString);
var count = context2.Entities.LongCount();
Assert.IsNotNull(count);
Assert.AreEqual(10000, count);
}