Last active
December 13, 2015 18:59
-
-
Save yetanotherchris/4959801 to your computer and use it in GitHub Desktop.
ORMS: NPersist example
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
[ClassMap(Table = "People")] | |
public class Person | |
{ | |
[PropertyMap(Columns = "EmployeeId", IsIdentity = true, IsAssignedBySource = true)] | |
public virtual Guid Id { get; set; } | |
[PropertyMap(Columns = "Name")] | |
public virtual string Name { get; set; } | |
[PropertyMap(Columns = "Email")] | |
public virtual string Email { get; set; } | |
} | |
// These are required in your assemblyinfo | |
[assembly: DomainMap(Source = "HelloWorldSource")] | |
[assembly: SourceMap(Compute = true, Name = "HelloWorldSource")] | |
public class PersonManager | |
{ | |
public static void Example() | |
{ | |
using (IContext context = GetContext()) | |
{ | |
// app.config/web.config settings would be used in GetContext() | |
Person person = (Person)context.CreateObject(typeof(Person)); | |
person.Id = Guid.NewGuid(); // This might not be needed | |
person.Name = "John"; | |
person.Email = "john@aol.com"; | |
context.Commit(); | |
Person person = (Person)context.GetObjectById(Guid.NewGuid()); // this would be a proper guid | |
IList<Person> list = context.GetObjects(typeof(Employee)); | |
// Filtered loading, basically SQL | |
NPathQuery query = new NPathQuery("Select * from Person Where Name=?", typeof(Person)); | |
query.Parameters.Add(new QueryParameter(DbType.AnsiString, "John")); | |
list = context.GetObjectsByNPath(query); | |
} | |
} | |
private IContext GetContext() | |
{ | |
IContext context = new Context(this.GetType().Assembly); | |
context.SetConnectionString("server=(local);uid=xxx;pwd=xxx;database=yourdb;"); | |
return context; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment