本文整理汇总了C#中Api.SaveChanges方法的典型用法代码示例。如果您正苦于以下问题:C# Api.SaveChanges方法的具体用法?C# Api.SaveChanges怎么用?C# Api.SaveChanges使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Api
的用法示例。
在下文中一共展示了Api.SaveChanges方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Init
/// <summary>
/// Initializes the module. This method should be used for
/// ensuring runtime resources and registering hooks.
/// </summary>
public void Init() {
using (var api = new Api()) {
// Ensure configuration params
var param = api.Params.GetSingle(where: p => p.Name == "akismet_apikey");
if (param == null) {
param = new Models.Param() {
Name = "akismet_apikey"
};
api.Params.Add(param);
}
param = api.Params.GetSingle(where: p => p.Name == "akismet_siteurl");
if (param == null) {
param = new Models.Param() {
Name = "akismet_siteurl",
};
api.Params.Add(param);
}
// Save changes
api.SaveChanges();
}
// Add configuration to the manager
Manager.Config.Blocks.Add(new Manager.Config.ConfigBlock("Blogging", "Akismet", new List<Manager.Config.ConfigRow>() {
new Manager.Config.ConfigRow(new List<Manager.Config.ConfigColumn>() {
new Manager.Config.ConfigColumn(new List<Manager.Config.ConfigItem>() {
new Manager.Config.ConfigString() {
Name = "API-key", Param = "akismet_apikey", Value = Config.Akismet.ApiKey
}
}),
new Manager.Config.ConfigColumn(new List<Manager.Config.ConfigItem>() {
new Manager.Config.ConfigString() {
Name = "Site URL", Param = "akismet_siteurl", Value = Config.Akismet.SiteUrl
}
})
})
}));
// Add model hooks
Hooks.Models.Comment.OnSave += (c) => {
if (!String.IsNullOrWhiteSpace(Config.Akismet.ApiKey)) {
//
// TODO: Maybe check if the comment exists in the database
//
if (akismet.VerifyKey()) {
if (akismet.CommentCheck(c)) {
App.Logger.Log(Log.LogLevel.WARNING, "Akismet: Comment was marked as spam by Akismet");
c.IsSpam = true;
}
} else {
App.Logger.Log(Log.LogLevel.ERROR, "Akismet: ApiKey verification failed.");
}
} else {
App.Logger.Log(Log.LogLevel.INFO, "Akismet: No api key configured. Skipping comment validation.");
}
};
}
示例2: TagRepository
public async Task TagRepository() {
var id = Guid.Empty;
// Insert tag
using (var api = new Api()) {
var tag = new Models.Tag() {
Name = "My tag",
};
api.Tags.Add(tag);
Assert.AreEqual(tag.Id.HasValue, true);
Assert.IsTrue(api.SaveChanges() > 0);
id = tag.Id.Value;
}
// Get by slug
using (var api = new Api()) {
var tag = await api.Tags.GetBySlugAsync("my-tag");
Assert.IsNotNull(tag);
Assert.AreEqual(tag.Name, "My tag");
}
// Update tag
using (var api = new Api()) {
var tag = await api.Tags.GetBySlugAsync("my-tag");
Assert.IsNotNull(tag);
tag.Name = "My updated tag";
api.Tags.Add(tag);
Assert.IsTrue(api.SaveChanges() > 0);
}
// Get by id
using (var api = new Api()) {
var tag = await api.Tags.GetByIdAsync(id);
Assert.IsNotNull(tag);
Assert.AreEqual(tag.Name, "My updated tag");
}
// Remove tag
using (var api = new Api()) {
var tag = await api.Tags.GetBySlugAsync("my-tag");
api.Tags.Remove(tag);
Assert.IsTrue(api.SaveChanges() > 0);
}
}
示例3: CategoryRepository
public async Task CategoryRepository() {
var id = Guid.Empty;
// Insert category
using (var api = new Api()) {
var cat = new Models.Category() {
Name = "My category",
};
api.Categories.Add(cat);
Assert.IsTrue(cat.Id.HasValue);
Assert.IsTrue(api.SaveChanges() > 0);
id = cat.Id.Value;
}
// Get by slug
using (var api = new Api()) {
var cat = await api.Categories.GetBySlugAsync("my-category");
Assert.IsNotNull(cat);
Assert.AreEqual(cat.Name, "My category");
}
// Update category
using (var api = new Api()) {
var cat = await api.Categories.GetBySlugAsync("my-category");
Assert.IsNotNull(cat);
cat.Name = "My updated category";
api.Categories.Add(cat);
Assert.IsTrue(api.SaveChanges() > 0);
}
// Get by id
using (var api = new Api()) {
var cat = await api.Categories.GetByIdAsync(id);
Assert.IsNotNull(cat);
Assert.AreEqual(cat.Name, "My updated category");
}
// Remove category
using (var api = new Api()) {
var cat = await api.Categories.GetBySlugAsync("my-category");
api.Categories.Remove(cat);
Assert.IsTrue(api.SaveChanges() > 0);
}
}
示例4: Run
/// <summary>
/// Test the alias repository.
/// </summary>
protected void Run() {
var id = Guid.Empty;
using (var api = new Api()) {
// Add new model
var model = new Models.Alias() {
OldUrl = "oldstuff.aspx?id=thisisalongunreadableandyglyurl",
NewUrl = "~/blog/my-new-permalink",
IsPermanent = true
};
api.Aliases.Add(model);
api.SaveChanges();
id = model.Id;
}
using (var api = new Api()) {
// Get model
var model = api.Aliases.GetSingle(id);
Assert.IsNotNull(model);
Assert.AreEqual("/oldstuff.aspx?id=thisisalongunreadableandyglyurl", model.OldUrl);
Assert.AreEqual("/blog/my-new-permalink", model.NewUrl);
Assert.AreEqual(true, model.IsPermanent);
// Update model
model.NewUrl = "/blog/welcome";
api.SaveChanges();
}
using (var api = new Api()) {
// Verify update
var model = api.Aliases.GetSingle(id);
Assert.IsNotNull(model);
Assert.AreEqual("/oldstuff.aspx?id=thisisalongunreadableandyglyurl", model.OldUrl);
Assert.AreEqual("/blog/welcome", model.NewUrl);
Assert.AreEqual(true, model.IsPermanent);
// Remove model
api.Aliases.Remove(model);
api.SaveChanges();
}
using (var api = new Api()) {
// Verify remove
var model = api.Aliases.GetSingle(id);
Assert.IsNull(model);
}
}
示例5: Run
/// <summary>
/// Test the category repository.
/// </summary>
protected void Run() {
using (var api = new Api()) {
// Add new model
var model = new Models.Block() {
Name = "My block",
Description = "My test block",
Body = "Lorem ipsum"
};
api.Blocks.Add(model);
api.SaveChanges();
}
using (var api = new Api()) {
// Get model
var model = api.Blocks.GetSingle(where: c => c.Slug == "my-block");
Assert.IsNotNull(model);
Assert.AreEqual("My block", model.Name);
Assert.AreEqual("My test block", model.Description);
Assert.AreEqual("Lorem ipsum", model.Body);
// Update model
model.Name = "Updated";
api.SaveChanges();
}
using (var api = new Api()) {
// Verify update
var model = api.Blocks.GetSingle(where: c => c.Slug == "my-block");
Assert.IsNotNull(model);
Assert.AreEqual("Updated", model.Name);
Assert.AreEqual("My test block", model.Description);
Assert.AreEqual("Lorem ipsum", model.Body);
// Remove model
api.Blocks.Remove(model);
api.SaveChanges();
}
using (var api = new Api()) {
// Verify remove
var model = api.Blocks.GetSingle(where: c => c.Slug == "my-block");
Assert.IsNull(model);
}
}
示例6: Save
/// <summary>
/// Saves the current edit model.
/// </summary>
public void Save(Api api) {
var newModel = false;
// Get or create category
var category = Id.HasValue ? api.Categories.GetSingle(Id.Value) : null;
if (category == null) {
category = new Piranha.Models.Category();
newModel = true;
}
// Map values
Mapper.Map<EditModel, Piranha.Models.Category>(this, category);
if (newModel)
api.Categories.Add(category);
api.SaveChanges();
this.Id = category.Id;
}
示例7: Run
/// <summary>
/// Test the post type repository.
/// </summary>
protected void Run() {
using (var api = new Api()) {
// Add new model
var model = new Models.PostType() {
Name = "Standard post",
Route = "post"
};
api.PostTypes.Add(model);
api.SaveChanges();
}
using (var api = new Api()) {
// Get model
var model = api.PostTypes.GetSingle(where: t => t.Slug == "standard-post");
Assert.IsNotNull(model);
Assert.AreEqual("Standard post", model.Name);
Assert.AreEqual("post", model.Route);
// Update model
model.Name = "Updated post";
api.SaveChanges();
}
using (var api = new Api()) {
// Verify update
var model = api.PostTypes.GetSingle(where: t => t.Slug == "standard-post");
Assert.IsNotNull(model);
Assert.AreEqual("Updated post", model.Name);
Assert.AreEqual("post", model.Route);
// Remove model
api.PostTypes.Remove(model);
api.SaveChanges();
}
using (var api = new Api()) {
// Verify remove
var model = api.PostTypes.GetSingle(where: t => t.Slug == "standard-post");
Assert.IsNull(model);
}
}
示例8: Run
/// <summary>
/// Test the param repository.
/// </summary>
protected void Run() {
using (var api = new Api()) {
// Add new model
var model = new Models.Param() {
Name = "my_param",
Value = "23"
};
api.Params.Add(model);
api.SaveChanges();
}
using (var api = new Api()) {
// Get model
var model = api.Params.GetSingle(where: p => p.Name == "my_param");
Assert.IsNotNull(model);
Assert.AreEqual("23", model.Value);
// Update model
model.Value = "32";
api.SaveChanges();
}
using (var api = new Api()) {
// Verify update
var model = api.Params.GetSingle(where: p => p.Name == "my_param");
Assert.IsNotNull(model);
Assert.AreEqual("32", model.Value);
// Remove model
api.Params.Remove(model);
api.SaveChanges();
}
using (var api = new Api()) {
// Verify remove
var model = api.Params.GetSingle(where: p => p.Name == "my_param");
Assert.IsNull(model);
}
}
示例9: Run
/// <summary>
/// Test the author repository.
/// </summary>
protected void Run() {
using (var api = new Api()) {
// Add new model
var model = new Models.Author() {
Name = "John Doe",
Email = "[email protected]"
};
api.Authors.Add(model);
api.SaveChanges();
}
using (var api = new Api()) {
// Get model
var model = api.Authors.GetSingle(where: a => a.Name == "John Doe");
Assert.IsNotNull(model);
Assert.AreEqual("[email protected]", model.Email);
// Update model
model.Name = "Sir John Doe";
api.SaveChanges();
}
using (var api = new Api()) {
// Verify update
var model = api.Authors.GetSingle(where: a => a.Email == "[email protected]");
Assert.IsNotNull(model);
Assert.AreEqual("Sir John Doe", model.Name);
// Remove model
api.Authors.Remove(model);
api.SaveChanges();
}
using (var api = new Api()) {
// Verify remove
var model = api.Authors.GetSingle(where: a => a.Email == "[email protected]");
Assert.IsNull(model);
}
}
示例10: Run
/// <summary>
/// Test the category repository.
/// </summary>
protected void Run() {
using (var api = new Api()) {
// Add new model
var model = new Models.Category() {
Title = "My category"
};
api.Categories.Add(model);
api.SaveChanges();
}
using (var api = new Api()) {
// Get model
var model = api.Categories.GetSingle(where: c => c.Slug == "my-category");
Assert.IsNotNull(model);
// Update model
model.Title = "Updated";
api.SaveChanges();
}
using (var api = new Api()) {
// Verify update
var model = api.Categories.GetSingle(where: c => c.Slug == "my-category");
Assert.IsNotNull(model);
Assert.AreEqual("Updated", model.Title);
// Remove model
api.Categories.Remove(model);
api.SaveChanges();
}
using (var api = new Api()) {
// Verify remove
var model = api.Categories.GetSingle(where: c => c.Slug == "my-category");
Assert.IsNull(model);
}
}
示例11: Add
public virtual ActionResult Add(Piranha.Models.Comment model) {
if (ModelState.IsValid) {
using (var api = new Api()) {
model.IP = HttpContext.Request.UserHostAddress;
model.UserAgent = HttpContext.Request.UserAgent.Substring(0, Math.Min(HttpContext.Request.UserAgent.Length, 128));
model.SessionID = Session.SessionID;
model.IsApproved = true;
if (User.Identity.IsAuthenticated)
model.UserId = User.Identity.Name;
if (User.Identity.IsAuthenticated && Config.Comments.ModerateAuthorized)
model.IsApproved = false;
else if (!User.Identity.IsAuthenticated && Config.Comments.ModerateAnonymous)
model.IsApproved = false;
api.Comments.Add(model);
api.SaveChanges();
}
var post = PostModel.GetById(model.PostId);
return Redirect("~/" + post.Type + "/" + post.Slug);
}
return null;
}
示例12: Init
/// <summary>
/// Initializes the module. This method should be used for
/// ensuring runtime resources and registering hooks.
/// </summary>
public void Init() {
using (var api = new Api()) {
// Ensure configuration params
var param = api.Params.GetSingle(where: p => p.Name == "feed_pagesize");
if (param == null) {
param = new Models.Param() {
Name = "feed_pagesize",
Value = "10"
};
api.Params.Add(param);
}
param = api.Params.GetSingle(where: p => p.Name == "feed_sitefeedtitle");
if (param == null) {
param = new Models.Param() {
Name = "feed_sitefeedtitle",
Value = "{SiteTitle} > Feed"
};
api.Params.Add(param);
}
param = api.Params.GetSingle(where: p => p.Name == "feed_archivefeedtitle");
if (param == null) {
param = new Models.Param() {
Name = "feed_archivefeedtitle",
Value = "{SiteTitle} > {PostType} Archive Feed"
};
api.Params.Add(param);
}
param = api.Params.GetSingle(where: p => p.Name == "feed_commentfeedtitle");
if (param == null) {
param = new Models.Param() {
Name = "feed_commentfeedtitle",
Value = "{SiteTitle} > Comments Feed"
};
api.Params.Add(param);
}
param = api.Params.GetSingle(where: p => p.Name == "feed_postfeedtitle");
if (param == null) {
param = new Models.Param() {
Name = "feed_postfeedtitle",
Value = "{SiteTitle} > {PostTitle} Comments Feed"
};
api.Params.Add(param);
}
// Save changes
api.SaveChanges();
}
// Add configuration to the manager
Manager.Config.Blocks.Add(new Manager.Config.ConfigBlock("Blogging", "Feed", new List<Manager.Config.ConfigRow>() {
new Manager.Config.ConfigRow(new List<Manager.Config.ConfigColumn>() {
new Manager.Config.ConfigColumn(new List<Manager.Config.ConfigItem>() {
new Manager.Config.ConfigString() {
Name = "Site title", Param = "feed_sitefeedtitle", Value = Config.Feed.SiteFeedTitle
},
new Manager.Config.ConfigString() {
Name = "Archive title", Param = "feed_archivefeedtitle", Value = Config.Feed.ArchiveFeedTitle
},
new Manager.Config.ConfigInteger() {
Name = "Page size", Param = "feed_pagesize", Value = Config.Feed.PageSize.ToString()
}
}),
new Manager.Config.ConfigColumn(new List<Manager.Config.ConfigItem>() {
new Manager.Config.ConfigString() {
Name = "Comment title", Param = "feed_commentfeedtitle", Value = Config.Feed.CommentFeedTitle
},
new Manager.Config.ConfigString() {
Name = "Post title", Param = "feed_postfeedtitle", Value = Config.Feed.PostFeedTitle
}
})
})
}));
// Add the feed handler
App.Handlers.Add("feed", new FeedHandler());
// Add UI rendering
Hooks.UI.Head.Render += (sb) => {
// Get current
var current = App.Env.GetCurrent();
// Render base feeds
var sTitle = HttpUtility.HtmlEncode(Config.Feed.SiteFeedTitle
.Replace("{SiteTitle}", Config.Site.Title));
var cTitle = HttpUtility.HtmlEncode(Config.Feed.CommentFeedTitle
.Replace("{SiteTitle}", Config.Site.Title));
sb.Append(String.Format(LINK_TAG, "alternate", "application/rss+xml", sTitle,
App.Env.AbsoluteUrl("~/feed")));
sb.Append(String.Format(LINK_TAG, "alternate", "application/rss+xml", cTitle,
App.Env.AbsoluteUrl("~/feed/comments")));
if (current.Type == ContentType.Archive) {
using (var api = new Api()) {
var type = api.PostTypes.GetSingle(current.Id);
//.........这里部分代码省略.........
示例13: Run
/// <summary>
/// Test the post repository.
/// </summary>
protected void Run() {
Models.PostType type = null;
Models.Author author = null;
Models.Post post = null;
using (var api = new Api()) {
// Add new post type
type = new Models.PostType() {
Name = "Test post",
Route = "post"
};
api.PostTypes.Add(type);
api.SaveChanges();
// Add new author
author = new Models.Author() {
Name = "Jane Doe",
Email = "[email protected]"
};
api.Authors.Add(author);
api.SaveChanges();
// Add new post
post = new Models.Post() {
TypeId = type.Id,
AuthorId = author.Id,
Title = "My test post",
Excerpt = "Read my first post.",
Body = "<p>Lorem ipsum</p>",
Published = DateTime.Now
};
api.Posts.Add(post);
api.SaveChanges();
}
using (var api = new Api()) {
// Get model
var model = api.Posts.GetSingle(where: p => p.Slug == "my-test-post");
Assert.IsNotNull(model);
Assert.AreEqual("Read my first post.", model.Excerpt);
Assert.AreEqual("<p>Lorem ipsum</p>", model.Body);
// Update model
model.Excerpt = "Updated post";
api.SaveChanges();
}
using (var api = new Api()) {
// Verify update
var model = api.Posts.GetSingle(where: p => p.Slug == "my-test-post");
Assert.IsNotNull(model);
Assert.AreEqual("Updated post", model.Excerpt);
Assert.AreEqual("<p>Lorem ipsum</p>", model.Body);
// Remove
api.Posts.Remove(model);
api.PostTypes.Remove(type.Id);
api.Authors.Remove(author.Id);
api.SaveChanges();
}
using (var api = new Api()) {
// Verify remove
post = api.Posts.GetSingle(where: p => p.Slug == "my-test-post");
type = api.PostTypes.GetSingle(type.Id);
author = api.Authors.GetSingle(author.Id);
Assert.IsNull(post);
Assert.IsNull(type);
Assert.IsNull(author);
}
}
示例14: Params
/// <summary>
/// Seed params.
/// </summary>
/// <param name="api">The current api</param>
public static void Params(Api api) {
var param = api.Params.GetSingle(where: p => p.Name == "site_title");
if (param == null) {
api.Params.Add(new Models.Param() {
Name = "site_title",
Value = "Piranha CMS"
});
}
param = api.Params.GetSingle(where: p => p.Name == "site_description");
if (param == null) {
api.Params.Add(new Models.Param() {
Name = "site_description",
Value = "Welcome to Piranha CMS"
});
}
param = api.Params.GetSingle(where: p => p.Name == "site_lastmodified");
if (param == null) {
api.Params.Add(new Models.Param() {
Name = "site_lastmodified",
Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
});
}
param = api.Params.GetSingle(where: p => p.Name == "archive_pagesize");
if (param == null) {
api.Params.Add(new Models.Param() {
Name = "archive_pagesize",
Value = "10"
});
}
param = api.Params.GetSingle(where: p => p.Name == "cache_expires");
if (param == null) {
api.Params.Add(new Models.Param() {
Name = "cache_expires",
Value = "0"
});
}
param = api.Params.GetSingle(where: p => p.Name == "cache_maxage");
if (param == null) {
api.Params.Add(new Models.Param() {
Name = "cache_maxage",
Value = "0"
});
}
param = api.Params.GetSingle(where: p => p.Name == "comment_moderate_authorized");
if (param == null) {
api.Params.Add(new Models.Param() {
Name = "comment_moderate_authorized",
Value = false.ToString()
});
}
param = api.Params.GetSingle(where: p => p.Name == "comment_moderate_anonymous");
if (param == null) {
api.Params.Add(new Models.Param() {
Name = "comment_moderate_anonymous",
Value = false.ToString()
});
}
param = api.Params.GetSingle(where: p => p.Name == "comment_notify_author");
if (param == null) {
api.Params.Add(new Models.Param() {
Name = "comment_notify_author",
Value = false.ToString()
});
}
param = api.Params.GetSingle(where: p => p.Name == "comment_notify_moderators");
if (param == null) {
api.Params.Add(new Models.Param() {
Name = "comment_notify_moderators",
Value = false.ToString()
});
}
param = api.Params.GetSingle(where: p => p.Name == "comment_moderators");
if (param == null) {
api.Params.Add(new Models.Param() {
Name = "comment_moderators",
Value = ""
});
}
api.SaveChanges();
}
示例15: Run
/// <summary>
/// Test the rating repository.
/// </summary>
protected void Run() {
var userId = Guid.NewGuid().ToString();
Models.PostType type = null;
Models.Author author = null;
Models.Post post = null;
using (var api = new Api()) {
// Add new post type
type = new Models.PostType() {
Name = "Rating post",
Route = "post"
};
api.PostTypes.Add(type);
api.SaveChanges();
// Add new author
author = new Models.Author() {
Name = "Jim Doe",
Email = "[email protected]"
};
api.Authors.Add(author);
api.SaveChanges();
// Add new post
post = new Models.Post() {
TypeId = type.Id,
AuthorId = author.Id,
Title = "My rated post",
Excerpt = "Read my first post.",
Body = "<p>Lorem ipsum</p>",
Published = DateTime.Now
};
api.Posts.Add(post);
api.SaveChanges();
}
using (var api = new Api()) {
// Add ratings
api.Ratings.AddRating(Models.RatingType.Star, post.Id, userId);
api.Ratings.AddRating(Models.RatingType.Like, post.Id, userId);
api.SaveChanges();
}
using (var api = new Api()) {
// Verify save
var model = Client.Models.PostModel.GetById(post.Id).WithRatings();
Assert.AreEqual(1, model.Ratings.Stars.Count);
Assert.AreEqual(1, model.Ratings.Likes.Count);
// Remove ratings
api.Ratings.RemoveRating(Models.RatingType.Star, post.Id, userId);
api.Ratings.RemoveRating(Models.RatingType.Like, post.Id, userId);
api.SaveChanges();
}
using (var api = new Api()) {
// Verify remove
var model = Client.Models.PostModel.GetById(post.Id).WithRatings();
Assert.AreEqual(0, model.Ratings.Stars.Count);
Assert.AreEqual(0, model.Ratings.Likes.Count);
// Remove
api.Posts.Remove(post.Id);
api.PostTypes.Remove(type.Id);
api.Authors.Remove(author.Id);
api.SaveChanges();
}
using (var api = new Api()) {
// Verify remove
post = api.Posts.GetSingle(where: p => p.Slug == "my-rated-post");
type = api.PostTypes.GetSingle(type.Id);
author = api.Authors.GetSingle(author.Id);
Assert.IsNull(post);
Assert.IsNull(type);
Assert.IsNull(author);
}
}