本文整理汇总了C#中TestDbContext类的典型用法代码示例。如果您正苦于以下问题:C# TestDbContext类的具体用法?C# TestDbContext怎么用?C# TestDbContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TestDbContext类属于命名空间,在下文中一共展示了TestDbContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ShouldNotUpdatePropertiesOfAnAssociatedEntity
public void ShouldNotUpdatePropertiesOfAnAssociatedEntity()
{
var node1 = new TestNode
{
Title = "New Node",
OneToOneAssociated = new OneToOneAssociatedModel { Title = "Associated Node" }
};
using (var context = new TestDbContext())
{
context.Nodes.Add(node1);
context.SaveChanges();
} // Simulate detach
node1.OneToOneAssociated.Title = "Updated Content";
using (var context = new TestDbContext())
{
context.UpdateGraph(node1);
context.SaveChanges();
var node2 = context.Nodes.Include(p => p.OneToOneAssociated).Single(p => p.Id == node1.Id);
Assert.IsNotNull(node2);
Assert.IsTrue(node2.OneToOneAssociated.OneParent == node2);
// should not delete it as it is associated and no cascade rule set.
Assert.IsTrue(node2.OneToOneAssociated.Title == "Associated Node");
}
}
示例2: ShouldNotUpdateEntitesWithinAnAssociatedCollection
public void ShouldNotUpdateEntitesWithinAnAssociatedCollection()
{
var node1 = new TestNode
{
Title = "New Node",
OneToManyAssociated = new List<OneToManyAssociatedModel>
{
new OneToManyAssociatedModel { Title = "First One" }
}
};
using (var context = new TestDbContext())
{
context.Nodes.Add(node1);
context.SaveChanges();
} // Simulate detach
node1.OneToManyAssociated.First().Title = "This should not overwrite value";
using (var context = new TestDbContext())
{
// Setup mapping
context.UpdateGraph(node1, map => map
.AssociatedCollection(p => p.OneToManyAssociated));
context.SaveChanges();
var node2 = context.Nodes.Include(p => p.OneToManyAssociated).Single(p => p.Id == node1.Id);
Assert.IsNotNull(node2);
Assert.IsTrue(node2.OneToManyAssociated.Single().Title == "First One");
}
}
示例3: Dispose
public void Dispose()
{
using (var dbContext = new TestDbContext())
{
dbContext.Database.Delete();
}
}
示例4: ShouldAddRelationToExistingAssociatedCollection
public void ShouldAddRelationToExistingAssociatedCollection()
{
var associated = new OneToManyAssociatedModel { Title = "Second One" };
var node1 = new TestNode
{
Title = "New Node",
OneToManyAssociated = new List<OneToManyAssociatedModel>
{
new OneToManyAssociatedModel { Title = "First One" }
}
};
using (var context = new TestDbContext())
{
context.Nodes.Add(node1);
context.OneToManyAssociatedModels.Add(associated);
context.SaveChanges();
} // Simulate detach
node1.OneToManyAssociated.Add(associated);
using (var context = new TestDbContext())
{
// Setup mapping
context.UpdateGraph(node1, map => map
.AssociatedCollection(p => p.OneToManyAssociated));
context.SaveChanges();
var node2 = context.Nodes.Include(p => p.OneToManyAssociated).Single(p => p.Id == node1.Id);
Assert.IsNotNull(node2);
Assert.IsTrue(node2.OneToManyAssociated.Count == 2);
}
}
示例5: ShouldSupportGuidKeys
public void ShouldSupportGuidKeys()
{
var model = new GuidTestNode();
using (var context = new TestDbContext())
{
context.GuidKeyModels.Add(model);
context.SaveChanges();
// http://stackoverflow.com/questions/5270721/using-guid-as-pk-with-ef4-code-first
Assert.IsTrue(Attribute.IsDefined(model.GetType().GetProperty("Id"), typeof(DatabaseGeneratedAttribute)));
Assert.IsNotNull(model.Id);
Assert.AreNotEqual(Guid.Empty, model.Id);
} // simulate detach
model.OneToOneOwned = new GuidOneToOneOwned();
using (var context = new TestDbContext())
{
model = context.UpdateGraph(model, map => map.OwnedEntity(g => g.OneToOneOwned));
context.SaveChanges();
Assert.IsNotNull(model.OneToOneOwned);
Assert.IsNotNull(model.OneToOneOwned.Id);
Assert.AreNotEqual(Guid.Empty, model.OneToOneOwned.Id);
}
}
示例6: ShouldSupportNullValuesInTree
public void ShouldSupportNullValuesInTree()
{
var node1 = new TestNode
{
Title = "New Node",
OneToOneOwned = null
};
using (var context = new TestDbContext())
{
context.Nodes.Add(node1);
context.SaveChanges();
} // Simulate detach
using (var context = new TestDbContext())
{
// Setup mapping
node1 = context.UpdateGraph(node1, map => map
.OwnedEntity(p => p.OneToOneOwned, with =>
with.OwnedEntity(p => p.OneToOneOneToOneOwned)));
context.SaveChanges();
context.Entry(node1).Reload();
Assert.IsTrue(node1.OneToOneOwned == null);
}
}
示例7: ShouldThrowDbUpdateConcurrencyExceptionWithEmptyRowVersion
public void ShouldThrowDbUpdateConcurrencyExceptionWithEmptyRowVersion()
{
TestNode node;
using (var db = new TestDbContext())
{
node = new TestNode
{
Title = "Hello",
OneToManyOwned = new List<OneToManyOwnedModel>
{
new OneToManyOwnedModel { Title = "Test1" },
new OneToManyOwnedModel { Title = "Test2" }
}
};
db.Nodes.Add(node);
db.SaveChanges();
}
using (var db = new TestDbContext())
{
node.OneToManyOwned.First().RowVersion = null;
db.UpdateGraph(node, map => map.OwnedCollection(p => p.OneToManyOwned));
db.SaveChanges();
}
}
示例8: ShouldAddNewAggregateRoot_Attached
public void ShouldAddNewAggregateRoot_Attached()
{
var associated = new OneToOneAssociatedModel { Title = "Associated" };
var manyAssociated = new OneToManyAssociatedModel { Title = "Associated" };
var node1 = new TestNode
{
Title = "New Node",
OneToManyOwned = new List<OneToManyOwnedModel>
{
new OneToManyOwnedModel { Title = "One" },
new OneToManyOwnedModel { Title = "Two" },
new OneToManyOwnedModel { Title = "Three" }
},
OneToManyAssociated = new List<OneToManyAssociatedModel>
{
manyAssociated
},
OneToOneOwned = new OneToOneOwnedModel { Title = "OneToOne" },
OneToOneAssociated = associated
};
using (var context = new TestDbContext())
{
context.OneToManyAssociatedModels.Add(manyAssociated);
context.OneToOneAssociatedModels.Add(associated);
node1 = context.UpdateGraph(node1);
context.SaveChanges();
Assert.IsNotNull(context.Nodes.SingleOrDefault(p => p.Id == node1.Id));
}
}
示例9: ShouldAddAssociatedWithoutChangingRequiredAssociate
public void ShouldAddAssociatedWithoutChangingRequiredAssociate()
{
var root = new RootEntity { RequiredAssociate = new RequiredAssociate(), Sources = new List<RootEntity>() };
var requiredAssociate = new RequiredAssociate();
using (var context = new TestDbContext())
{
context.RootEntities.Add(root);
context.RequiredAssociates.Add(requiredAssociate);
context.SaveChanges();
} // Simulate detach
var expectedAssociateId = requiredAssociate.Id;
var owned = new RootEntity { RequiredAssociate = requiredAssociate };
root.Sources.Add(owned);
using (var context = new TestDbContext())
{
root = context.UpdateGraph(root, map => map.AssociatedCollection(r => r.Sources));
context.SaveChanges();
var ownedAfterSave = root.Sources.FirstOrDefault();
Assert.IsNotNull(ownedAfterSave);
Assert.IsNotNull(ownedAfterSave.RequiredAssociate);
Assert.AreEqual(expectedAssociateId, ownedAfterSave.RequiredAssociate.Id);
var ownedReloaded = context.RootEntities.Single(r => r.Id == ownedAfterSave.Id);
Assert.IsNotNull(ownedReloaded.RequiredAssociate);
Assert.AreEqual(expectedAssociateId, ownedReloaded.RequiredAssociate.Id);
}
}
示例10: ShouldAddNewItemInOwnedCollection
public void ShouldAddNewItemInOwnedCollection()
{
var node1 = new TestNode
{
Title = "New Node",
OneToManyOwned = new List<OneToManyOwnedModel>
{
new OneToManyOwnedModel { Title = "Hello" }
}
};
using (var context = new TestDbContext())
{
context.Nodes.Add(node1);
context.SaveChanges();
} // Simulate detach
var newModel = new OneToManyOwnedModel { Title = "Hi" };
node1.OneToManyOwned.Add(newModel);
using (var context = new TestDbContext())
{
// Setup mapping
context.UpdateGraph(node1, map => map
.OwnedCollection(p => p.OneToManyOwned));
context.SaveChanges();
var node2 = context.Nodes.Include(p => p.OneToManyOwned).Single(p => p.Id == node1.Id);
Assert.IsNotNull(node2);
Assert.IsTrue(node2.OneToManyOwned.Count == 2);
var owned = context.OneToManyOwnedModels.Single(p => p.Id == newModel.Id);
Assert.IsTrue(owned.OneParent == node2 && owned.Title == "Hi");
}
}
示例11: ShouldSupportMultipleKeys
public void ShouldSupportMultipleKeys()
{
var model = new MultiKeyModel
{
Title = "Hello",
Date = DateTime.Now,
KeyPart1 = "A123",
KeyPart2 = "A234"
};
using (var context = new TestDbContext())
{
context.MultiKeyModels.Add(model);
context.SaveChanges();
} // simulate detach
model.Date = DateTime.Parse("01/01/2010");
using (var context = new TestDbContext())
{
model = context.UpdateGraph(model);
context.SaveChanges();
context.Entry(model).Reload();
Assert.IsTrue(model.Date == DateTime.Parse("01/01/2010"));
}
}
示例12: ShouldAddRelationIfPreviousValueWasNullWithCycle
public void ShouldAddRelationIfPreviousValueWasNullWithCycle()
{
GroupedTestNode two;
GroupedTestNode one;
using (var context = new TestDbContext())
{
var group = new NodeGroup();
context.NodeGroups.Add(group);
context.SaveChanges();
one = new GroupedTestNode { Group = group };
context.Nodes.Add(one);
context.SaveChanges();
two = new GroupedTestNode { Group = group };
context.Nodes.Add(two);
context.SaveChanges();
Assert.AreEqual(2, group.Members.Count);
} // Simulate detach
using (var context = new TestDbContext())
{
one.Two = two;
// Setup mapping
context.UpdateGraph(one, map => map.AssociatedEntity(o => o.Two));
context.SaveChanges();
var oneReloaded = context.Nodes.OfType<GroupedTestNode>().Include("Two").Single(n => n.Id == one.Id);
Assert.IsNotNull(oneReloaded.Two);
Assert.AreEqual(two.Id, oneReloaded.Two.Id);
}
}
示例13: ShouldAddRelationIfPreviousValueWasNull
public void ShouldAddRelationIfPreviousValueWasNull()
{
var node1 = new TestNode { Title = "New Node" };
var associated = new OneToOneAssociatedModel { Title = "Associated Node" };
using (var context = new TestDbContext())
{
context.Nodes.Add(node1);
context.OneToOneAssociatedModels.Add(associated);
context.SaveChanges();
} // Simulate detach
node1.OneToOneAssociated = associated;
using (var context = new TestDbContext())
{
// Setup mapping
context.UpdateGraph(node1, map => map
.AssociatedEntity(p => p.OneToOneAssociated));
context.SaveChanges();
var node2 = context.Nodes.Include(p => p.OneToOneAssociated).Single(p => p.Id == node1.Id);
Assert.IsNotNull(node2);
Assert.IsTrue(node2.OneToOneAssociated.OneParent == node2);
}
}
示例14: Initialize
public void Initialize()
{
//Arrange
Context = new TestDbContext();
var uow = new UnitofWork(Context);
Controller = new UnitofWorkPatternController(uow);
}
示例15: ShouldBeAbleToVisitExpressionsStoredAsProperties
public void ShouldBeAbleToVisitExpressionsStoredAsProperties()
{
var node1 = new TestNode
{
Title = "One",
OneToOneOwned = new OneToOneOwnedModel { Title = "Hello" }
};
using (var context = new TestDbContext())
{
context.Nodes.Add(node1);
context.SaveChanges();
} // Simulate detach
node1.OneToOneOwned.Title = "Hey2";
Lambda = (p => p.OneToOneOwned);
Expression<Func<IUpdateConfiguration<TestNode>, dynamic>> exp = map => map.OwnedEntity(Lambda);
using (var context = new TestDbContext())
{
// Setup mapping
context.UpdateGraph(node1, exp);
context.SaveChanges();
Assert.IsTrue(context.Nodes
.Include(p => p.OneToOneOwned)
.Single(p => p.Id == node1.Id)
.OneToOneOwned.Title == "Hey2");
}
}