Created
May 24, 2013 02:30
-
-
Save eperedo/5640893 to your computer and use it in GitHub Desktop.
Example Servicestack OrmLite Blob Text Objects
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
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 | |
} | |
} | |
} |
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
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; } | |
} |
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
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