Entity Framework 5.0追加削除(カスケード)
2773 ワード
Entity Framework 5.0追加削除(カスケード)
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestInsert()
{
using (TestEntities1 context = new TestEntities1())
{
User user = new User();
user.ID = 10;
user.UName = "Jim";
user.Address = new Address();
user.Address.ID = 7;
user.Address.Address1 = " ";
context.Set<User>().Add(user);
DbEntityEntry<Address> entry = context.Entry<Address>(user.Address);
entry.State = System.Data.EntityState.Modified;
//entry.State = System.Data.EntityState.Added;
DbEntityEntry<User> entry2 = context.Entry<User>(user);
entry2.State = System.Data.EntityState.Added;
context.SaveChanges();
}
}
[TestMethod]
public void TestUpdate()
{
using (TestEntities1 context = new TestEntities1())
{
User user = new User();
user.ID = 10;
user.UName = "Jim";
user.Address = new Address();
user.Address.ID = 7;
user.Address.Address1 = " ";
context.Set<User>().Add(user);
DbEntityEntry<Address> entry = context.Entry<Address>(user.Address);
entry.State = System.Data.EntityState.Modified;
DbEntityEntry<User> entry2 = context.Entry<User>(user);
entry2.State = System.Data.EntityState.Modified;
context.SaveChanges();
}
}
[TestMethod]
public void TestSelect()
{
using (TestEntities1 context = new TestEntities1())
{
var user = context.UserAndSubs().Where(u => u.ID == 10)
.Select(u => u).ToList();
}
}
[TestMethod]
public void TestDelete()
{
using (TestEntities1 context = new TestEntities1())
{
var user = context.UserAndSubs().Where(u => u.ID == 10).First();
context.Set<User>().Add(user);
context.Set<Address>().Remove(user.Address);
context.Entry<User>(user).State = System.Data.EntityState.Deleted;
context.SaveChanges();
}
}
}