本文整理汇总了C#中OperationContext.OpenSession方法的典型用法代码示例。如果您正苦于以下问题:C# OperationContext.OpenSession方法的具体用法?C# OperationContext.OpenSession怎么用?C# OperationContext.OpenSession使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OperationContext
的用法示例。
在下文中一共展示了OperationContext.OpenSession方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SearchLogins
//Login admin - add interface for admins
public SearchResults<ILogin> SearchLogins(OperationContext context, LoginSearch search)
{
var utcNow = context.App.TimeService.UtcNow;
var session = context.OpenSession();
search = search.DefaultIfNull(take: 20, defaultOrderBy: "UserName");
var where = session.NewPredicate<ILogin>()
.AndIfNotEmpty(search.TenantId, lg => lg.TenantId == search.TenantId.Value)
.AndIfNotEmpty(search.UserName, lg => lg.UserName.StartsWith(search.UserName))
.AndIfNotEmpty(search.UserId, lg => lg.UserId == search.UserId)
.AndIfNotEmpty(search.ExpiringBefore, lg => lg.Expires != null && lg.Expires < search.ExpiringBefore.Value)
.AndIfNotEmpty(search.CreatedAfter, lg => lg.CreatedOn >= search.CreatedAfter.Value)
.AndIfNotEmpty(search.CreatedBefore, lg => lg.CreatedOn <= search.CreatedBefore.Value)
.AndIf(search.EnabledOnly, lg => (lg.Flags & LoginFlags.Disabled) == 0)
.AndIf(search.SuspendedOnly, lg => (lg.Flags & LoginFlags.Suspended) == 0 && lg.SuspendedUntil > utcNow);
if (!string.IsNullOrWhiteSpace(search.Email)) {
var factorHash = Util.StableHash(search.Email.Trim().ToLowerInvariant());
var subQuery = session.EntitySet<ILoginExtraFactor>().Where(f => f.InfoHash == factorHash).Select(f => f.Login.Id);
where = where.And(lg => subQuery.Contains(lg.Id));
};
var result = session.ExecuteSearch(where, search);
if(LoginExtensions.CheckSuspensionEnded(result.Results, utcNow)) {
}
return result;
}
示例2: TestMultiTenant
public void TestMultiTenant() {
// Multi-tenant test is available only for MS SQL Server.
if (SetupHelper.ServerType != DbServerType.MsSql)
return;
const string Books2 = "VitaBooks2";
var app = SetupHelper.BooksApp;
var connString2 = SetupHelper.ConnectionString.Replace("VitaBooks", Books2);
var mainDbStt = SetupHelper.DbSettings;
var dbSettings2 = new DbSettings(mainDbStt.ModelConfig, connString2, upgradeMode: DbUpgradeMode.Always, dataSourceName: Books2);
Vita.UnitTests.Common.TestUtil.DropSchemaObjects(dbSettings2);
app.ConnectTo(dbSettings2);
//read from new store
var ctx1 = new OperationContext(app); // DataSourceName=Default
// The second context will point to another database
var ctx2 = new OperationContext(app);
ctx2.DataSourceName = Books2;
var session = ctx2.OpenSession();
var books = session.EntitySet<IBook>().ToList();
Assert.AreEqual(0, books.Count, "Expected no books in new store");
//Let's create a pub and a book in VitaBooks2
var newPub = session.NewPublisher("VBooks");
var newBook = session.NewBook(BookEdition.Hardcover, BookCategory.Programming, "Voodoo Programming", "Voodoo science in code", newPub, DateTime.Now, 10m);
session.SaveChanges();
//check we inserted them in VitaBooks2
session = ctx2.OpenSession();
var pubCopy = session.EntitySet<IPublisher>().FirstOrDefault(p => p.Name == newPub.Name);
Assert.IsNotNull(pubCopy, "Publisher not found in VitaBooks2");
var bookCopy = session.EntitySet<IBook>().FirstOrDefault(b => b.Title == newBook.Title);
Assert.IsNotNull(bookCopy, "Book not found.");
//Check that it does not exist in 'main' VitaBooks database
session = ctx1.OpenSession(); //open session in VitaBooks
var pub = session.EntitySet<IPublisher>().FirstOrDefault(p => p.Name == newPub.Name);
Assert.IsNull(pub, "Publisher should not exist in VitaBooks");
var book = session.EntitySet<IBook>().FirstOrDefault(b => b.Title == newBook.Title);
Assert.IsNull(book, "Book should not exist in VitaBooks.");
//Verify both data sources share the DbModel object
var dsService = app.DataAccess;
var ds1 = dsService.GetDataSource(ctx1);
var ds2 = dsService.GetDataSource(ctx2);
Assert.IsNotNull(ds1, "Default data source not found.");
Assert.IsNotNull(ds2, "Books2 data source not found.");
Assert.AreEqual(ds1.Database.DbModel, ds2.Database.DbModel, "Db models are not shared.");
}