本文整理汇总了C#中OperationContext.OpenSecureSession方法的典型用法代码示例。如果您正苦于以下问题:C# OperationContext.OpenSecureSession方法的具体用法?C# OperationContext.OpenSecureSession怎么用?C# OperationContext.OpenSecureSession使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OperationContext
的用法示例。
在下文中一共展示了OperationContext.OpenSecureSession方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestCanDelete
public void TestCanDelete()
{
var app = SetupHelper.BooksApp;
var session = app.OpenSession();
// create some book to delete
var msPub = session.EntitySet<IPublisher>().First(p => p.Name.StartsWith("MS"));
var fsBook = session.NewBook(BookEdition.Paperback, BookCategory.Programming, "F# programming", "Introduction to F#", msPub, DateTime.Now, 20);
var john = session.EntitySet<IAuthor>().First(a => a.LastName == "Sharp");
fsBook.Authors.Add(john);
session.SaveChanges();
var fsBkId = fsBook.Id;
// Test CanDelete. There are no orders for Fs book, so we may be able to delete it
// We can delete a book even if there are links for it in BookAuthor link table (BookAuthor.Book has CascadeDelete attr)
// so the result here should be true
session = app.OpenSession();
fsBook = session.GetEntity<IBook>(fsBkId);
Type[] blockingEntities;
var canDelete = session.CanDeleteEntity(fsBook, out blockingEntities);
Assert.IsTrue(canDelete, "CanDelete F# book failed.");
session.DeleteEntity(fsBook);
session.SaveChanges();
//check it is deleted; we also check here that entity cache works correctly and does not keep object.
fsBook = session.GetEntity<IBook>(fsBkId);
Assert.IsNull(fsBook, "F# not deleted.");
var allBooks = session.GetEntities<IBook>();
//Now check Publisher: mspub has associated books, so it should not be allowed to be deleted.
msPub = session.EntitySet<IPublisher>().Single(p => p.Name.StartsWith("MS"));
canDelete = session.CanDeleteEntity(msPub, out blockingEntities);
Assert.IsFalse(canDelete, "Error: MS publisher should not be allowed to be deleted.");
Assert.IsTrue(blockingEntities != null && blockingEntities.Length > 0, "Blocking entities empty.");
//Still try to delete
session.DeleteEntity(msPub);
session.LogMessage("\r\n-- TEST: expecting integrity violation exception ...."); //to make clear in Log that exc is expected
var dex = TestUtil.ExpectDataAccessException(() => { session.SaveChanges(); });
Assert.AreEqual(DataAccessException.SubTypeIntegrityViolation, dex.SubType, "Expected integrity violation exception subtype.");
//Bug fix: CanDelete blows up in secure session
var lindaTheEditor = session.EntitySet<IUser>().First(u => u.UserName == "Linda").ToUserInfo();
var opContext = new OperationContext(SetupHelper.BooksApp, lindaTheEditor);
var secSession = opContext.OpenSecureSession();
msPub = secSession.EntitySet<IPublisher>().Single(p => p.Name.StartsWith("MS"));
canDelete = secSession.CanDeleteEntity(msPub, out blockingEntities);
Assert.IsFalse(canDelete, "Expected CanDelete to be false.");
}
示例2: OpenSecureSession
private ISecureSession OpenSecureSession(UserInfo user)
{
var opContext = new OperationContext(SetupHelper.BooksApp, user);
var session = opContext.OpenSecureSession();
return session;
}