Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ORMS: NPersist example
[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