本文整理汇总了C#中DocumentStore.OpenSession方法的典型用法代码示例。如果您正苦于以下问题:C# DocumentStore.OpenSession方法的具体用法?C# DocumentStore.OpenSession怎么用?C# DocumentStore.OpenSession使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DocumentStore
的用法示例。
在下文中一共展示了DocumentStore.OpenSession方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DtcTransactions
public DtcTransactions()
{
var store = new DocumentStore();
using (var session = store.OpenSession())
{
#region transaction_scope_usage
using (var transaction = new TransactionScope())
{
Employee employee = session.Load<Employee>("employees/1");
employee.FirstName = "James";
session.SaveChanges(); // will create HTTP request
session.Delete(employee);
session.SaveChanges(); // will create HTTP request
transaction.Complete(); // will commit the transaction
}
#endregion
}
#region default_transaction_recovery_storage
store.TransactionRecoveryStorage = new VolatileOnlyTransactionRecoveryStorage();
#endregion
#region local_directory_transaction_recovery_storage
store.TransactionRecoveryStorage = new LocalDirectoryTransactionRecoveryStorage(@"C:\tx_recovery_data");
#endregion
}
示例2: Should_update_retrieved_entity
public void Should_update_retrieved_entity()
{
using (var documentStore = new DocumentStore())
{
documentStore.Database = DbName;
documentStore.AddMap(new CompanyMap());
documentStore.Initialise();
var session1 = documentStore.OpenSession();
var company = new Company {Name = "Company 1"};
session1.Store(company);
var companyId = company.Id;
var session2 = documentStore.OpenSession();
var companyFound = session2.Load<Company>(companyId);
companyFound.Name = "New Name";
session2.SaveChanges();
Assert.AreEqual("New Name", session2.Load<Company>(companyId).Name);
}
}
示例3: BewareOf
public BewareOf()
{
var store = new DocumentStore();
using (var session = store.OpenSession())
{
#region session_value_types
Employee employee = session.Load<Employee>(9); // get "employees/9"
session.Delete<Employee>(12); // delete "employees/12"
#endregion
}
}
示例4: CreateDbAndLoad
public void CreateDbAndLoad()
{
const string DbName = "CreationTest";
using (var documentStore = new DocumentStore { Url = "http://localhost:8080/databases/" + DbName }.Initialize())
{
documentStore.DatabaseCommands.EnsureDatabaseExists(DbName);
using (var session = documentStore.OpenSession())
{
session.Load<Order>("1");
}
}
}
示例5: Should_map_Entity_Id_to_document_during_store
public void Should_map_Entity_Id_to_document_during_store()
{
using (var documentStore = new DocumentStore())
{
documentStore.Database = DbName;
documentStore.AddMap(new CompanyMap());
documentStore.Initialise();
var session = documentStore.OpenSession();
var company = new Company() { Name = "Company 1" };
session.Store(company);
Assert.AreNotEqual(Guid.Empty, company.Id);
}
}
示例6: GetByIdTests
public GetByIdTests()
{
_testStore = new StoreInfo();
_store = new TestStore();
dummyCars = new Fixture().CreateMany<Car>().ToList();
using (var session = _store.OpenSession())
{
foreach (var car in dummyCars)
{
session.Store(car);
}
session.SaveChanges();
}
}
示例7: Should_Load_entity_back_with_document_Id_mapped_to_Id
public void Should_Load_entity_back_with_document_Id_mapped_to_Id()
{
using (var documentStore = new DocumentStore())
{
documentStore.Database = DbName;
documentStore.AddMap(new CompanyMap());
documentStore.Initialise();
var documentId = documentStore.DocSharp.Store(new Company { Name = "Company NAme" });
var session = documentStore.OpenSession();
var companyFound = session.Load<Company>(documentId.Id);
Assert.AreEqual(companyFound.Id, documentId.Id);
}
}
示例8: Should_update_stored_entity
public void Should_update_stored_entity()
{
using (var documentStore = new DocumentStore())
{
documentStore.Database = DbName;
documentStore.AddMap(new CompanyMap());
documentStore.Initialise();
var session = documentStore.OpenSession();
var company = new Company { Name = "Company 1" };
session.Store(company);
company.Name = "Company 2";
session.SaveChanges();
Assert.AreEqual("Company 2", session.Load<Company>(company.Id).Name);
}
}
示例9: Sample
public void Sample()
{
using (var documentStore = new DocumentStore())
{
using (var session = documentStore.OpenSession())
{
#region secure_for
session.SecureFor("Authorization/Users/DrHowser", "Hospitalization/Authorize");
Authorization.Patient mary = session.Load<Authorization.Patient>("Patients/MaryMallon");
mary.AuthorizeHospitalization();
session.SaveChanges();
#endregion
}
}
}
示例10: WorkingWithDocumentIds
public WorkingWithDocumentIds()
{
var store = new DocumentStore();
var session = store.OpenSession();
#region session_id_not_provided
Order order = new Order
{
Id = null // value not provided
};
session.Store(order);
#endregion
#region session_get_document_id
string orderId = session.Advanced.GetDocumentId(order); // "orders/1"
#endregion
#region session_empty_string_id
Order orderEmptyId = new Order
{
Id = string.Empty // database will create a GUID value for it
};
session.Store(orderEmptyId);
session.SaveChanges();
string guidId = session.Advanced.GetDocumentId(orderEmptyId); // "6778c231-180b-4715-aad4-253c4c6027a4"
#endregion
#region session_semantic_id_1
Product product = new Product
{
Id = "products/ravendb",
Name = "RavenDB"
};
session.Store(product);
#endregion
#region session_semantic_id_2
session.Store(new Product() { Name = "RavenDB" }, "products/ravendb");
#endregion
#region session_identity_id
session.Store(new Company()
{
Id = "companies/"
});
session.Store(new Company()
{
Id = "companies/"
});
session.SaveChanges();
#endregion
#region commands_autogenerated_guid
PutResult result = store.DatabaseCommands.Put(null, null, new RavenJObject(){ {"Name", "RavenDB"}}, new RavenJObject());
string key = result.Key; // "9ce12df5-1027-4704-b2cf-d312e9ea0e59"
#endregion
#region commands_identity
result = store
.DatabaseCommands
.Put(
"products/",
null,
new RavenJObject { { "Name", "RavenDB" } },
new RavenJObject());
string identityKey = result.Key; // "products/1"
#endregion
#region commands_identity_generate
long identity = store.DatabaseCommands.NextIdentityFor("products");
result = store
.DatabaseCommands
.Put(
"products/" + identity,
null,
new RavenJObject { { "Name", "RavenDB" } },
new RavenJObject());
#endregion
#region commands_identity_set
store.DatabaseCommands.SeedIdentityFor("products", 42);
#endregion
}
示例11: CleanUpDB
private void CleanUpDB(DocumentStore documentStore)
{
// This can fail the very first time the application runs as it will not yet have created the necessary schema objects,
// so we can just quietly move on
try
{
var docSession = documentStore.OpenSession();
var configInfo = docSession.Load<Config>("DinnerParty/Config");
if(configInfo == null)
{
configInfo = new Config();
configInfo.Id = "DinnerParty/Config";
configInfo.LastTruncateDate = DateTime.Now.AddHours(-48);
//No need to delete data if config doesnt exist but setup ready for next time
docSession.Store(configInfo);
docSession.SaveChanges();
return;
}
if((DateTime.Now - configInfo.LastTruncateDate).TotalHours < 24)
return;
long docCount = 0, dbSize = 0;
using(var cmd = docSession.Connection.CreateCommand())
{
var dinnerTableName = documentStore.Schema.MappingFor(typeof(Dinner)).Table.QualifiedName;
cmd.CommandText = $"SELECT COUNT(*) FROM {dinnerTableName}";
docCount = (long)cmd.ExecuteScalar();
}
using(var cmd = docSession.Connection.CreateCommand())
{
var dinnerPartyDbName = docSession.Connection.Database;
cmd.CommandText = $"SELECT pg_database_size('{dinnerPartyDbName}')";
dbSize = (long)cmd.ExecuteScalar();
configInfo.LastTruncateDate = DateTime.Now;
docSession.SaveChanges();
}
//If database size >15mb or 1000 documents delete documents older than a week
if(docCount > 1000 || dbSize > 15000000) //its actually 14.3mb but goood enough
{
docSession.DeleteWhere<Dinner>(dp => dp.LastModified < DateTime.Now.AddDays(-7));
}
}
catch(Exception ex)
{
LogManager.GetCurrentClassLogger().Warn(ex, "Failed to clean up database");
}
}
示例12: TypeSpecific
public TypeSpecific()
{
var store = new DocumentStore();
#region eployees_custom_convention
store.Conventions.RegisterIdConvention<Employee>(
(dbname, commands, employee) => string.Format("employees/{0}/{1}", employee.LastName, employee.FirstName));
#endregion
#region eployees_custom_async_convention
store.Conventions.RegisterAsyncIdConvention<Employee>(
(dbname, commands, employee) => new CompletedTask<string>(
string.Format("employees/{0}/{1}", employee.LastName, employee.FirstName)));
#endregion
#region eployees_custom_convention_example
using (IDocumentSession session = store.OpenSession())
{
session.Store(new Employee
{
FirstName = "James",
LastName = "Bond"
});
session.SaveChanges();
}
#endregion
#region eployees_custom_convention_inheritance
using (IDocumentSession session = store.OpenSession())
{
session.Store(new Employee // employees/Smith/Adam
{
FirstName = "Adam",
LastName = "Smith"
});
session.Store(new EmployeeManager // employees/Jones/David
{
FirstName = "David",
LastName = "Jones"
});
session.SaveChanges();
}
#endregion
#region custom_convention_inheritance_2
store.Conventions.RegisterIdConvention<Employee>(
(dbname, commands, employee) => string.Format("employees/{0}/{1}", employee.LastName, employee.FirstName));
store.Conventions.RegisterIdConvention<EmployeeManager>(
(dbname, commands, employee) => string.Format("managers/{0}/{1}", employee.LastName, employee.FirstName));
using (IDocumentSession session = store.OpenSession())
{
session.Store(new Employee // employees/Smith/Adam
{
FirstName = "Adam",
LastName = "Smith"
});
session.Store(new EmployeeManager // managers/Jones/David
{
FirstName = "David",
LastName = "Jones"
});
session.SaveChanges();
}
#endregion
#region id_generation_on_load_1
store.Conventions.RegisterIdConvention<EntityWithIntegerId>(
(databaseName, commands, entity) => "ewi/" + entity.Id);
#endregion
#region id_generation_on_load_2
store.Conventions.RegisterIdLoadConvention<EntityWithIntegerId>(id => "ewi/" + id);
using (IDocumentSession session = store.OpenSession())
{
EntityWithIntegerId entity = session.Load<EntityWithIntegerId>(1); // will load 'ewi/1' document
}
#endregion
}
示例13: Main
static void Main(string[] args)
{
const int records = 10000;
var watch = new Stopwatch();
Console.WriteLine("Creating {0} records", records);
DocumentStore store = new DocumentStore("Server=127.0.0.1;Port=5432;User id=store_user;password=my super secret password;database=store;");
var cars = GenerateCars(records);
Console.WriteLine("Generated {0} records", records);
Console.WriteLine("Saving {0} records to database", records);
watch.Start();
using (var session = store.OpenSession())
{
foreach(var car in cars)
{
session.Store(car);
}
session.SaveChanges();
}
watch.Stop();
var rate = records / watch.Elapsed.TotalSeconds;
Console.WriteLine("Saved {0} records to datatbase in {1}s @ {2} p/s rate", records, watch.Elapsed.TotalSeconds, rate);
Console.WriteLine("Press any key to do query test....");
Console.ReadLine();
List<Car> fords;
using (var session = store.OpenSession())
{
watch.Reset();
watch.Start();
fords = session.Query<Car>().Where(c => c.Make == "Ford").ToList();
watch.Stop();
}
foreach (var ford in fords)
{
Console.WriteLine("Id: {0}, Make: {1}", ford.Id, ford.Make);
}
Console.WriteLine("Query took {0}ms", watch.Elapsed.TotalMilliseconds);
List<Car> holdens;
using (var session = store.OpenSession())
{
watch.Reset();
watch.Start();
holdens = session.Query<Car>().Where(c => c.Make == "Holden").ToList();
watch.Stop();
}
foreach (var holden in holdens)
{
Console.WriteLine("Id: {0}, Make: {1}", holden.Id, holden.Make);
}
Console.WriteLine("Query took {0}ms", watch.Elapsed.TotalMilliseconds);
//cleanup
using (var session = store.OpenSession())
{
session.DeleteAll<Car>();
}
Console.ReadLine();
}