Skip to content

Instantly share code, notes, and snippets.

@eperedo
Created May 24, 2013 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eperedo/5640893 to your computer and use it in GitHub Desktop.
Save eperedo/5640893 to your computer and use it in GitHub Desktop.
Example Servicestack OrmLite Blob Text Objects
public class DbConfig
{
public readonly string ConnectionString = ConfigurationManager.ConnectionStrings["TestDb"].ConnectionString;
public readonly OrmLiteConnectionFactory DbFactory;
public DbConfig()
{
DbFactory = new OrmLiteConnectionFactory(ConnectionString, PostgreSqlDialect.Provider);
}
public void Save(){
using (IDbConnection db = DbFactory.OpenDbConnection())
{
db.CreateTableIfNotExists<Document>();
var newDoc = new Document();
newDoc.Id = Guid.NewGuid();
newDoc.FileName = "Doc 1" + DateTime.Now.Ticks;
var properties = new List<Property>();
properties.Add(new Property { Id = 1, Name = "Property 1", Value = "Value 1" });
properties.Add(new Property { Id = 2, Name = "Property 2", Value = "Value 2" });
properties.Add(new Property { Id = 3, Name = "Property 3", Value = "Value 3" });
newDoc.Properties = properties;
db.Insert(newDoc); //Insert document with 3 properties
var result = db.GetById<Document>(newDoc.Id); //Get document from database
//Make changes to update/insert
result.Properties[0].Name = "propiedad 1"; //Update property 1
result.Properties[0].Value = "Another Value 1"; //Update property 1
result.Properties[2].Name = "propiedad 3"; //Update property 1
result.Properties.Add(new Property{Id = 4, Name = "Property 4",Value = "Value 4"}); //Add a new one
db.Save(result);//Insert/Update object
}
}
}
public class Document
{
public Document()
{
Properties = new List<Property>();
}
public Guid Id { get; set; }
public string FileName { get; set; }
public List<Property> Properties { get; set; }
}
public class Property
{
public int Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment