本文整理汇总了C#中TestDbContext.Set方法的典型用法代码示例。如果您正苦于以下问题:C# TestDbContext.Set方法的具体用法?C# TestDbContext.Set怎么用?C# TestDbContext.Set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TestDbContext
的用法示例。
在下文中一共展示了TestDbContext.Set方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InsertTestItems
public static void InsertTestItems(TestDbContext context)
{
var item1 = new Item { Name = "Test Item 1", Description = "Test Item 1" };
var item2 = new Item { Name = "Test Item 2", Description = "Test Item 2" };
context.Set<Item>().Add(item1);
context.Set<Item>().Add(item2);
}
示例2: ShouldSupportOwnedCollectionWithNestedModels
public void ShouldSupportOwnedCollectionWithNestedModels()
{
// setup
var oneToOneAssociations = new List<OneToManyOneToOneAssociatedModel>();
var oneToManyAssociations = new List<OneToManyOneToManyAssociatedModel>();
var node1 = new TestNode
{
Title = "New Node",
OneToManyOwned = new List<OneToManyOwnedModel>()
};
for (var i = 0; i < 3; i++)
{
node1.OneToManyOwned.Add(new OneToManyOwnedModel
{
OneToManyOneToOneAssociated = new OneToManyOneToOneAssociatedModel { Title = "Hello" },
OneToManyOneToOneOwned = new OneToManyOneToOneOwnedModel { Title = "Hello" },
OneToManyOneToManyAssociated = new List<OneToManyOneToManyAssociatedModel>
{
new OneToManyOneToManyAssociatedModel { Title = "Hello" },
new OneToManyOneToManyAssociatedModel { Title = "Hello" }
},
OneToManyOneToManyOwned = new List<OneToManyOneToManyOwnedModel>
{
new OneToManyOneToManyOwnedModel { Title = "Hello" },
new OneToManyOneToManyOwnedModel { Title = "Hello" },
new OneToManyOneToManyOwnedModel { Title = "Hello" }
}
});
oneToOneAssociations.Add(new OneToManyOneToOneAssociatedModel { Title = "Associated Update" });
oneToManyAssociations.Add(new OneToManyOneToManyAssociatedModel { Title = "Many Associated Update" + i });
}
using (var context = new TestDbContext())
{
foreach (var association in oneToOneAssociations)
{
context.Set<OneToManyOneToOneAssociatedModel>().Add(association);
}
foreach (var association in oneToManyAssociations)
{
context.Set<OneToManyOneToManyAssociatedModel>().Add(association);
}
context.Nodes.Add(node1);
context.SaveChanges();
} // Simulate detach
// change
var collection = node1.OneToManyOwned;
for (var i = 0; i < collection.Count; i++)
{
var element = collection.Skip(i).First();
element.OneToManyOneToOneOwned.Title = "Updated" + i;
element.OneToManyOneToOneAssociated = oneToOneAssociations[i];
var owned = element.OneToManyOneToManyOwned;
owned.Remove(owned.First());
owned.First().Title = "Updated" + i;
owned.Skip(1).First().Title = "Updated 2" + i;
owned.Add(new OneToManyOneToManyOwnedModel { Title = "A new one" + i });
var associated = element.OneToManyOneToManyAssociated;
associated.Remove(associated.First());
associated.Add(oneToManyAssociations[i]);
}
using (var context = new TestDbContext())
{
context.UpdateGraph(node1);
context.SaveChanges();
// assert
node1 = context.Nodes.Single();
var list = node1.OneToManyOwned.ToList();
for (var i = 0; i < collection.Count; i++)
{
var element = list[i];
Assert.IsNotNull(element);
Assert.IsTrue(element.OneToManyOneToOneOwned.Title == "Updated" + i);
Assert.IsTrue(element.OneToManyOneToOneAssociated.Title == "Associated Update");
var ownershipList = element.OneToManyOneToManyOwned.ToList();
Assert.IsTrue(ownershipList.Count == 3);
Assert.IsTrue(ownershipList[0].Title == "Updated" + i);
Assert.IsTrue(ownershipList[1].Title == "Updated 2" + i);
Assert.IsTrue(ownershipList[2].Title == "A new one" + i);
var associatedList = element.OneToManyOneToManyAssociated.ToList();
Assert.IsTrue(associatedList.Count == 2);
Assert.IsTrue(associatedList[0].Title == "Hello");
Assert.IsTrue(associatedList[1].Title == "Many Associated Update" + i);
}
}
}
示例3: ShouldSupportOwnedEntityWithNestedModels
public void ShouldSupportOwnedEntityWithNestedModels()
{
// setup
var oneToOneAssociated = new OneToOneOneToOneAssociatedModel { Title = "Associated Update" };
var oneToManyAssociated = new OneToOneOneToManyAssociatedModel { Title = "Many Associated Update" };
var node1 = new TestNode
{
Title = "New Node",
OneToOneOwned = new OneToOneOwnedModel
{
OneToOneOneToOneAssociated = new OneToOneOneToOneAssociatedModel { Title = "Hello" },
OneToOneOneToOneOwned = new OneToOneOneToOneOwnedModel { Title = "Hello" },
OneToOneOneToManyAssociated = new List<OneToOneOneToManyAssociatedModel>
{
new OneToOneOneToManyAssociatedModel { Title = "Hello" },
new OneToOneOneToManyAssociatedModel { Title = "Hello" }
},
OneToOneOneToManyOwned = new List<OneToOneOneToManyOwnedModel>
{
new OneToOneOneToManyOwnedModel { Title = "Hello" },
new OneToOneOneToManyOwnedModel { Title = "Hello" },
new OneToOneOneToManyOwnedModel { Title = "Hello" }
}
}
};
using (var context = new TestDbContext())
{
context.Set<OneToOneOneToOneAssociatedModel>().Add(oneToOneAssociated);
context.Set<OneToOneOneToManyAssociatedModel>().Add(oneToManyAssociated);
context.Nodes.Add(node1);
context.SaveChanges();
} // Simulate detach
// change
node1.OneToOneOwned.OneToOneOneToOneOwned.Title = "Updated";
node1.OneToOneOwned.OneToOneOneToOneAssociated = oneToOneAssociated;
var owned = node1.OneToOneOwned.OneToOneOneToManyOwned;
owned.Remove(owned.First());
owned.First().Title = "Updated";
owned.Skip(1).First().Title = "Updated 2";
owned.Add(new OneToOneOneToManyOwnedModel { Title = "A new one" });
var associated = node1.OneToOneOwned.OneToOneOneToManyAssociated;
associated.Remove(associated.First());
associated.Add(oneToManyAssociated);
using (var context = new TestDbContext())
{
context.UpdateGraph(node1);
context.SaveChanges();
var updated = context.Set<OneToOneOwnedModel>().Single();
Assert.IsNotNull(updated);
Assert.IsTrue(updated.OneToOneOneToOneOwned.Title == "Updated");
Assert.IsTrue(updated.OneToOneOneToOneAssociated.Title == "Associated Update");
var ownershipList = updated.OneToOneOneToManyOwned.ToList();
Assert.IsTrue(ownershipList.Count == 3);
Assert.IsTrue(ownershipList[0].Title == "Updated");
Assert.IsTrue(ownershipList[1].Title == "Updated 2");
Assert.IsTrue(ownershipList[2].Title == "A new one");
var associatedList = updated.OneToOneOneToManyAssociated.ToList();
Assert.IsTrue(associatedList.Count == 2);
Assert.IsTrue(associatedList[0].Title == "Hello");
Assert.IsTrue(associatedList[1].Title == "Many Associated Update");
}
}