示例#1
0
		public async Task<TextBlob> AddText(string text)
		{
			if (text == null)
				return new TextBlob
				{
					Hash = null,
					Text = null
				};

			if (text.Length > MaxTextSize)
				text = text.Substring(0, MaxTextSize);

			var hash = GetHash(text);
			var blob = db.Texts.Find(hash);
			if (blob != null)
				return blob;

			blob = new TextBlob
			{
				Hash = hash,
				Text = text
			};
			db.Texts.AddOrUpdate(blob);

			try
			{
				await db.SaveChangesAsync();
			}
			catch (DbEntityValidationException e)
			{
				throw new Exception(
					string.Join("\r\n",
					e.EntityValidationErrors.SelectMany(v => v.ValidationErrors).Select(err => err.PropertyName + " " + err.ErrorMessage)));
			}
			return blob;
		}
		private static string GetText(TextBlob blob)
		{
			return blob == null ? null : blob.Text;
		}