本文整理汇总了C#中Table.CreateIfNotExists方法的典型用法代码示例。如果您正苦于以下问题:C# Table.CreateIfNotExists方法的具体用法?C# Table.CreateIfNotExists怎么用?C# Table.CreateIfNotExists使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Table
的用法示例。
在下文中一共展示了Table.CreateIfNotExists方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CqlClient_Timestamp
public void CqlClient_Timestamp()
{
var config = new MappingConfiguration()
.Define(new Map<Song>().PartitionKey(s => s.Id).TableName("song_insert"));
//Use linq to create the table
var table = new Table<Song>(_session, config);
table.CreateIfNotExists();
var mapper = new Mapper(_session, config);
var song = new Song
{
Id = Guid.NewGuid(),
Artist = "The Who",
Title = "Substitute",
ReleaseDate = DateTimeOffset.UtcNow
};
//Set timestamp to 1 day ago
var timestamp = DateTimeOffset.Now.Subtract(TimeSpan.FromDays(1));
mapper.Insert(song, true, CqlQueryOptions.New().SetTimestamp(timestamp));
//query for timestamp in a column of the record
var cqlLowerCasePartitionKey = "SELECT WRITETIME (Artist) AS timestamp FROM " + table.Name + " WHERE Id = " + song.Id + ";";
var rows = _session.Execute(cqlLowerCasePartitionKey).GetRows().ToList();
Assert.AreEqual(1, rows.Count);
var creationTimestamp = rows[0].GetValue<long>("timestamp");
Assert.NotNull(creationTimestamp);
//Timestamp retrieved is in macroseconds. Converting it to milliseconds
Assert.AreEqual(TypeSerializer.SinceUnixEpoch(timestamp).Ticks / 10, rows[0].GetValue<object>("timestamp"));
}
示例2: Versioner
public Versioner(ISession session, ILogger<Version> logger)
{
_logger = logger;
_logger.Debug("Define global mappings");
MappingConfiguration.Global.Define<PocoMapper>();
_logger.Debug("Create mapper and table instances");
_mapper = new Mapper(session);
var table = new Table<DatabaseVersion>(session);
table.CreateIfNotExists();
}
示例3: CqlClientConfiguration_UseIndividualMappingClassType_StaticMappingClass
public void CqlClientConfiguration_UseIndividualMappingClassType_StaticMappingClass()
{
var config = new MappingConfiguration().Define(new ManyDataTypesPocoMappingCaseSensitive());
var table = new Table<ManyDataTypesPoco>(_session, config);
table.CreateIfNotExists();
var mapper = new Mapper(_session, config);
ManyDataTypesPoco manyTypesInstance = ManyDataTypesPoco.GetRandomInstance();
mapper.Insert(manyTypesInstance);
string cqlSelect = string.Format("SELECT * from \"{0}\" where \"{1}\"='{2}'", table.Name, "StringType", manyTypesInstance.StringType);
ManyDataTypesPoco.KeepTryingSelectAndAssert(mapper, cqlSelect, new List<ManyDataTypesPoco>() { manyTypesInstance });
}
示例4: CqlClientConfiguration_UseIndividualMappingGeneric_StaticMappingClass_
public void CqlClientConfiguration_UseIndividualMappingGeneric_StaticMappingClass_()
{
var config = new MappingConfiguration().Define(new ManyDataTypesPocoMappingCaseSensitive());
var table = new Table<ManyDataTypesPoco>(_session, config);
Assert.AreNotEqual(table.Name, table.Name.ToLower()); // make sure the case sensitivity rule is being used
table.CreateIfNotExists();
var mapper = new Mapper(_session, config);
var manyTypesInstance = ManyDataTypesPoco.GetRandomInstance();
mapper.Insert(manyTypesInstance);
var instancesQueried = mapper.Fetch<ManyDataTypesPoco>().ToList();
Assert.AreEqual(1, instancesQueried.Count);
manyTypesInstance.AssertEquals(instancesQueried[0]);
}
示例5: Counter_Success
public void Counter_Success()
{
var config = new AttributeBasedTypeDefinition(typeof(PocoWithCounterAttribute));
var table = new Table<PocoWithCounterAttribute>(_session, new MappingConfiguration().Define(config));
table.CreateIfNotExists();
var cqlClient = new Mapper(_session, new MappingConfiguration().Define(config));
List<PocoWithCounterAttribute> counterPocos = new List<PocoWithCounterAttribute>();
for (int i = 0; i < 10; i++)
{
counterPocos.Add(
new PocoWithCounterAttribute()
{
KeyPart1 = Guid.NewGuid(),
KeyPart2 = (decimal)123,
});
}
int counterIncrements = 100;
string updateStr = String.Format("UPDATE \"{0}\" SET \"{1}\"=\"{1}\" + 1 WHERE \"{2}\"=? and \"{3}\"=?", table.Name.ToLower(), "counter", "keypart1", "keypart2");
var updateSession = _session.Prepare(updateStr);
foreach (PocoWithCounterAttribute pocoWithCounter in counterPocos)
{
var boundStatement = updateSession.Bind(new object[] { pocoWithCounter.KeyPart1, pocoWithCounter.KeyPart2 });
for (int j = 0; j < counterIncrements; j++)
_session.Execute(boundStatement);
pocoWithCounter.Counter += counterIncrements;
}
List<PocoWithCounterAttribute> countersQueried = cqlClient.Fetch<PocoWithCounterAttribute>().ToList();
foreach (PocoWithCounterAttribute pocoWithCounterExpected in counterPocos)
{
bool counterFound = false;
foreach (PocoWithCounterAttribute pocoWithCounterActual in countersQueried)
{
if (pocoWithCounterExpected.KeyPart1 == pocoWithCounterActual.KeyPart1)
{
Assert.AreEqual(pocoWithCounterExpected.KeyPart2, pocoWithCounterExpected.KeyPart2);
Assert.AreEqual(pocoWithCounterExpected.Counter, pocoWithCounterExpected.Counter);
counterFound = true;
}
}
Assert.IsTrue(counterFound, "Counter with first key part: " + pocoWithCounterExpected.KeyPart1 + " was not found!");
}
}
示例6: LinqUpdate_Single_Async
public void LinqUpdate_Single_Async()
{
// Setup
Table<Movie> table = new Table<Movie>(_session, new MappingConfiguration());
table.CreateIfNotExists();
Movie movieToUpdate = _movieList[1];
var expectedMovie = new Movie(movieToUpdate.Title, movieToUpdate.Director, "something_different_" + Randomm.RandomAlphaNum(10), movieToUpdate.MovieMaker, 1212);
table.Where(m => m.Title == movieToUpdate.Title && m.MovieMaker == movieToUpdate.MovieMaker && m.Director == movieToUpdate.Director)
.Select(m => new Movie { Year = expectedMovie.Year, MainActor = expectedMovie.MainActor })
.Update()
.Execute();
List<Movie> actualMovieList = table.ExecuteAsync().Result.ToList();
Assert.AreEqual(_movieList.Count, actualMovieList.Count());
Assert.IsFalse(Movie.ListContains(_movieList, expectedMovie));
Movie.AssertListContains(actualMovieList, expectedMovie);
}
示例7: CqlClientConfiguration_UseIndividualMappingClassType_StaticMappingClass
public void CqlClientConfiguration_UseIndividualMappingClassType_StaticMappingClass()
{
// Use separate keyspace to avoid interfering with other tests
_uniqueKsName = TestUtils.GetUniqueKeyspaceName();
_session.CreateKeyspace(_uniqueKsName);
_session.ChangeKeyspace(_uniqueKsName);
var config = new MappingConfiguration().Define(new ManyDataTypesPocoMappingCaseSensitive());
var table = new Table<ManyDataTypesPoco>(_session, config);
table.CreateIfNotExists();
var mapper = new Mapper(_session, config);
ManyDataTypesPoco manyTypesInstance = ManyDataTypesPoco.GetRandomInstance();
mapper.Insert(manyTypesInstance);
string cqlSelect = string.Format("SELECT * from \"{0}\" where \"{1}\"='{2}'", table.Name, "StringType", manyTypesInstance.StringType);
ManyDataTypesPoco.KeepTryingSelectAndAssert(mapper, cqlSelect, new List<ManyDataTypesPoco>() { manyTypesInstance });
}
示例8: Versioner
public Versioner(ISession session, ILogger<Version> logger)
{
_logger = logger;
_logger.Debug("Define global mappings");
lock (_registrationLock)
{
if (!_didRegisterMapping)
{
MappingConfiguration.Global.Define<PocoMapper>();
_didRegisterMapping = true;
}
}
_logger.Debug("Create mapper and table instances");
_mapper = new Mapper(session);
var table = new Table<DatabaseVersion>(session);
table.CreateIfNotExists();
}
示例9: CqlClientConfiguration_UseIndividualMappingGeneric_StaticMappingClass
public void CqlClientConfiguration_UseIndividualMappingGeneric_StaticMappingClass()
{
// Use separate keyspace to avoid interfering with other tests
_uniqueKsName = TestUtils.GetUniqueKeyspaceName();
_session.CreateKeyspace(_uniqueKsName);
_session.ChangeKeyspace(_uniqueKsName);
var config = new MappingConfiguration().Define(new ManyDataTypesPocoMappingCaseSensitive());
var table = new Table<ManyDataTypesPoco>(_session, config);
Assert.AreNotEqual(table.Name, table.Name.ToLower()); // make sure the case sensitivity rule is being used
table.CreateIfNotExists();
var mapper = new Mapper(_session, config);
var manyTypesInstance = ManyDataTypesPoco.GetRandomInstance();
mapper.Insert(manyTypesInstance);
var instancesQueried = mapper.Fetch<ManyDataTypesPoco>().ToList();
Assert.AreEqual(instancesQueried.Count, 1);
manyTypesInstance.AssertEquals(instancesQueried[0]);
}
示例10: TableCreate_CreateIfNotExists_KeyspaceOverride_NoSuchKeyspace
public void TableCreate_CreateIfNotExists_KeyspaceOverride_NoSuchKeyspace()
{
string uniqueTableName = TestUtils.GetUniqueTableName();
string uniqueKsName = TestUtils.GetUniqueKeyspaceName();
string expectedErrMsg = string.Format("Cannot add (column family|table) '{0}' to non existing keyspace '{1}'.", uniqueTableName, uniqueKsName);
Table<AllDataTypesEntity> table = new Table<AllDataTypesEntity>(_session, new MappingConfiguration(), uniqueTableName, uniqueKsName);
try
{
table.CreateIfNotExists();
Assert.Fail("Expected Exception was not thrown!");
}
catch (InvalidConfigurationInQueryException e)
{
StringAssert.IsMatch(expectedErrMsg, e.Message);
}
}
示例11: LinqUpdate_Batch
public void LinqUpdate_Batch()
{
// Setup
Table<Movie> table = new Table<Movie>(_session, new MappingConfiguration());
table.CreateIfNotExists();
Movie movieToUpdate1 = _movieList[1];
Movie movieToUpdate2 = _movieList[2];
BatchStatement batch = new BatchStatement();
var expectedMovie1 = new Movie(movieToUpdate1.Title, movieToUpdate1.Director, "something_different_" + Randomm.RandomAlphaNum(10), movieToUpdate1.MovieMaker, 1212);
var update1 = table.Where(m => m.Title == movieToUpdate1.Title && m.MovieMaker == movieToUpdate1.MovieMaker && m.Director == movieToUpdate1.Director)
.Select(m => new Movie { Year = expectedMovie1.Year, MainActor = expectedMovie1.MainActor })
.Update();
batch.Add(update1);
var expectedMovie2 = new Movie(movieToUpdate2.Title, movieToUpdate2.Director, "also_something_different_" + Randomm.RandomAlphaNum(10), movieToUpdate2.MovieMaker, 1212);
var update2 = table.Where(m => m.Title == movieToUpdate2.Title && m.MovieMaker == movieToUpdate2.MovieMaker && m.Director == movieToUpdate2.Director)
.Select(m => new Movie { Year = expectedMovie2.Year, MainActor = expectedMovie2.MainActor })
.Update();
batch.Add(update2);
table.GetSession().Execute(batch);
List<Movie> actualMovieList = table.Execute().ToList();
Assert.AreEqual(_movieList.Count, actualMovieList.Count());
Assert.AreNotEqual(expectedMovie1.MainActor, expectedMovie2.MainActor);
Assert.IsFalse(Movie.ListContains(_movieList, expectedMovie1));
Assert.IsFalse(Movie.ListContains(_movieList, expectedMovie2));
Movie.AssertListContains(actualMovieList, expectedMovie1);
Movie.AssertListContains(actualMovieList, expectedMovie2);
}
示例12: TableCreate_CreateIfNotExists_TwoTablesSameName_TwoKeyspacesDifferentNames_KeyspaceOverride
public void TableCreate_CreateIfNotExists_TwoTablesSameName_TwoKeyspacesDifferentNames_KeyspaceOverride()
{
// Setup first table
string sharedTableName = typeof (AllDataTypesEntity).Name;
var mappingConfig = new MappingConfiguration().Define(new Map<AllDataTypesEntity>().TableName(sharedTableName).CaseSensitive().PartitionKey(c => c.StringType));
Table<AllDataTypesEntity> table = new Table<AllDataTypesEntity>(_session, mappingConfig);
table.CreateIfNotExists();
Assert.IsTrue(TestUtils.TableExists(_session, _uniqueKsName, sharedTableName));
WriteReadValidate(table);
// Create second table with same name in new keyspace
string newUniqueKsName = TestUtils.GetUniqueKeyspaceName();
_session.CreateKeyspace(newUniqueKsName);
Assert.AreNotEqual(_uniqueKsName, newUniqueKsName);
Table<AllDataTypesEntity> table2 = new Table<AllDataTypesEntity>(_session, mappingConfig, sharedTableName, newUniqueKsName);
table2.CreateIfNotExists();
Assert.IsTrue(TestUtils.TableExists(_session, newUniqueKsName, sharedTableName));
WriteReadValidate(table2);
}
示例13: TableCreate_CreateIfNotExists_TwoTablesSameName_TwoDifferentKeyspaces_ChangeKeyspace
public void TableCreate_CreateIfNotExists_TwoTablesSameName_TwoDifferentKeyspaces_ChangeKeyspace()
{
Table<AllDataTypesEntity> table1 = new Table<AllDataTypesEntity>(_session, new MappingConfiguration());
table1.CreateIfNotExists();
WriteReadValidate(table1);
// Create in second keyspace
string newUniqueKsName = TestUtils.GetUniqueKeyspaceName();
_session.CreateKeyspace(newUniqueKsName);
_session.ChangeKeyspace(newUniqueKsName);
Table<AllDataTypesEntity> table2 = new Table<AllDataTypesEntity>(_session, new MappingConfiguration());
//Assert.AreNotEqual(table1.KeyspaceName, table2.KeyspaceName); // KeyspaceName is not being assigned, this may not be correct
Assert.AreEqual(table1.Name, table2.Name);
table2.CreateIfNotExists();
WriteReadValidate(table2);
}
示例14: TableCreate_CreateIfNotExist
public void TableCreate_CreateIfNotExist()
{
Table<AllDataTypesEntity> table = new Table<AllDataTypesEntity>(_session, new MappingConfiguration());
table.CreateIfNotExists();
WriteReadValidate(table);
}
示例15: CqlClientConfiguration_UseIndividualMappings_EmptyTypeDefinitionList
public void CqlClientConfiguration_UseIndividualMappings_EmptyTypeDefinitionList()
{
// Setup
var config = new MappingConfiguration().Define(new Map<ManyDataTypesPoco>()
.PartitionKey(c => c.StringType));
var table = new Table<ManyDataTypesPoco>(_session, config);
table.CreateIfNotExists();
// validate default lower-casing
Assert.AreNotEqual(typeof(ManyDataTypesPoco).Name.ToLower(), typeof(ManyDataTypesPoco).Name);
Assert.AreNotEqual(table.Name.ToLower(), table.Name);
Assert.AreEqual(typeof(ManyDataTypesPoco).Name.ToLower(), table.Name.ToLower());
// Test
var mapper = new Mapper(_session, config);
ManyDataTypesPoco manyTypesInstance = ManyDataTypesPoco.GetRandomInstance();
mapper.Insert(manyTypesInstance);
// Verify results
string cqlSelect = string.Format("SELECT * from \"{0}\" where \"{1}\"='{2}'", table.Name.ToLower(), "stringtype", manyTypesInstance.StringType);
ManyDataTypesPoco.KeepTryingSelectAndAssert(mapper, cqlSelect, new List<ManyDataTypesPoco>() { manyTypesInstance });
}