Created
May 17, 2013 10:03
-
-
Save sitereactor/5598175 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Umbraco.Core; | |
using Umbraco.Core.Persistence; | |
using Umbraco.Core.Persistence.DatabaseAnnotations; | |
namespace Umbraco.Examples | |
{ | |
/// <summary> | |
/// Class that shows the use of creating a new table and inserting a new ArticlePoco. | |
/// This class is only intended for demo'ing and should not be used as it. | |
/// </summary> | |
public static class CreatingArticles | |
{ | |
public static void CreateArticleTable() | |
{ | |
var db = new DatabaseTrouble(); | |
db.CreateTable(typeof(ArticlePoco)); | |
} | |
public static void CreateDummyArticle() | |
{ | |
var db = new DatabaseTrouble(); | |
var poco = new ArticlePoco{ Date = DateTime.Now, Text = "This is a new Article!"}; | |
db.InsertData(poco); | |
} | |
} | |
/// <summary> | |
/// Class that acts as a shortcut to a few database operations | |
/// </summary> | |
public class DatabaseTrouble | |
{ | |
private readonly UmbracoDatabase _database; | |
public DatabaseTrouble() | |
{ | |
_database = ApplicationContext.Current.DatabaseContext.Database; | |
} | |
/// <summary> | |
/// Creates a database table for the passed in type | |
/// </summary> | |
/// <param name="typeOfPoco">Type of the Poco to create</param> | |
public void CreateTable(Type typeOfPoco) | |
{ | |
_database.CreateTable(false, typeOfPoco); | |
} | |
/// <summary> | |
/// Inserts a new ArticlePoco (without an Id - will be autoincremented) | |
/// </summary> | |
/// <param name="poco"></param> | |
public void InsertData(ArticlePoco poco) | |
{ | |
_database.Insert(poco); | |
} | |
/// <summary> | |
/// Updates an existing ArticlePoco. | |
/// </summary> | |
/// <remarks>Must have an Id before this is valid</remarks> | |
/// <param name="poco"></param> | |
public void UpdateData(ArticlePoco poco) | |
{ | |
_database.Update(poco); | |
} | |
/// <summary> | |
/// Deletes an ArticlePoco | |
/// </summary> | |
/// <param name="poco"></param> | |
public void Delete(ArticlePoco poco) | |
{ | |
_database.Delete(poco); | |
} | |
/// <summary> | |
/// Deletes an ArticlePoco by its Id | |
/// </summary> | |
/// <param name="id"></param> | |
public void Delete(int id) | |
{ | |
_database.Delete<ArticlePoco>(id); | |
} | |
} | |
/// <summary> | |
/// Poco class decorated with attributes needed to create a new table in the database | |
/// </summary> | |
[TableName("Articles")] | |
[PrimaryKey("Id")] | |
[ExplicitColumns] | |
public class ArticlePoco | |
{ | |
[Column("Id")] | |
[PrimaryKeyColumn(Name = "PK_Id")] | |
public int Id { get; set; } | |
[Column("Date")] | |
public DateTime Date { get; set; } | |
[Column("ArticleText")] | |
[NullSetting(NullSetting = NullSettings.Null)] | |
[SpecialDbType(SpecialDbTypes.NTEXT)] | |
public string Text { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment