本文整理汇总了C#中Mapper.First方法的典型用法代码示例。如果您正苦于以下问题:C# Mapper.First方法的具体用法?C# Mapper.First怎么用?C# Mapper.First使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mapper
的用法示例。
在下文中一共展示了Mapper.First方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Insert_With_TTL
public void Insert_With_TTL()
{
var config = new MappingConfiguration()
.Define(new Map<Song>().PartitionKey(s => s.Id).TableName("song_insert"));
//Use linq to create the table
new Table<Song>(_session, config).CreateIfNotExists();
var mapper = new Mapper(_session, config);
var song = new Song
{
Id = Guid.NewGuid(),
Artist = "The Who",
Title = "Substitute",
ReleaseDate = DateTimeOffset.UtcNow
};
mapper.Insert(song, true, 5);
var notExpiredSong = mapper.First<Song>("WHERE id = ?", song.Id);
Assert.NotNull(notExpiredSong);
Assert.AreEqual(song.Id, notExpiredSong.Id);
Assert.AreEqual(song.Artist, notExpiredSong.Artist);
Assert.AreEqual(song.Title, notExpiredSong.Title);
Thread.Sleep(6000);
var expiredSong = mapper.FirstOrDefault<Song>("WHERE id = ?", song.Id);
Assert.Null(expiredSong);
}
示例2: UpdateIf_Applied_Test
public void UpdateIf_Applied_Test()
{
var config = new MappingConfiguration()
.Define(new Map<Song>().PartitionKey(s => s.Id).TableName("song_update_if"));
//Use linq to create the table
new Table<Song>(_session, config).Create();
var mapper = new Mapper(_session, config);
var song = new Song { Id = Guid.NewGuid(), Artist = "Cream", Title = "Crossroad", ReleaseDate = DateTimeOffset.Parse("1970/1/1")};
//It is the first song there, it should apply it
mapper.Insert(song);
const string query = "SET artist = ?, title = ? WHERE id = ? IF releasedate = ?";
var appliedInfo = mapper.UpdateIf<Song>(Cql.New(query, song.Artist, "Crossroad2", song.Id, song.ReleaseDate));
Assert.True(appliedInfo.Applied);
Assert.Null(appliedInfo.Existing);
//Following times, it should not apply the mutation as the condition is not valid
appliedInfo = mapper.UpdateIf<Song>(Cql.New(query, song.Artist, "Crossroad3", song.Id, DateTimeOffset.Now));
Assert.False(appliedInfo.Applied);
Assert.NotNull(appliedInfo.Existing);
Assert.AreEqual(song.ReleaseDate, appliedInfo.Existing.ReleaseDate);
Assert.AreEqual("Crossroad2", mapper.First<Song>("WHERE id = ?", song.Id).Title);
}
示例3: Insert_Without_Nulls_Test
public void Insert_Without_Nulls_Test()
{
var config = new MappingConfiguration()
.Define(new Map<Song>().PartitionKey(s => s.Id).TableName("song_insert"));
//Use linq to create the table
new Table<Song>(_session, config).CreateIfNotExists();
var mapper = new Mapper(_session, config);
var song = new Song
{
Id = Guid.NewGuid(),
Artist = "The Who",
Title = "Substitute",
ReleaseDate = DateTimeOffset.UtcNow
};
mapper.Insert(song);
var storedSong = mapper.First<Song>("WHERE id = ?", song.Id);
Assert.AreEqual(song.Artist, storedSong.Artist);
//do NOT insert nulls
mapper.Insert(new Song { Id = song.Id, Artist = null, Title = "Substitute 2", ReleaseDate = DateTimeOffset.UtcNow}, false);
//it should have the new title but the artist should still be the same (not null)
storedSong = mapper.First<Song>("WHERE id = ?", song.Id);
Assert.NotNull(storedSong.Artist);
Assert.AreEqual(song.Artist, storedSong.Artist);
Assert.AreEqual("Substitute 2", storedSong.Title);
//Now insert nulls
mapper.Insert(new Song { Id = song.Id, Artist = null, Title = "Substitute 3", ReleaseDate = DateTimeOffset.UtcNow }, true);
//it should have the new title and the artist should be null
storedSong = mapper.First<Song>("WHERE id = ?", song.Id);
Assert.Null(storedSong.Artist);
Assert.AreEqual("Substitute 3", storedSong.Title);
}