Skip to content

Cormo.Data.EntityFramework

hendryluk edited this page Feb 11, 2015 · 2 revisions

Getting Started

Nuget:

Install-Package Cormo.Data.EntityFramework

Ready to use

public class Product
{
   public string Name {get; set;}
}
[RestController]
public class ProductsController
{
  [Inject] IDataSet<Product> _products;

  [Route("product"), HttpPost]
  public void AddProduct(string name)
  {
     _products.Add(new Product {Name = name});
  }
}

By default, it will create a SqlServer database file: App_Data/default.mdf. To override this, add a "Default" connection string on your web.config/app.config. To use a different connection name (other than default), or to have multiple connection strings, you can use [EntityContext] attribute at injection time. E.g.:

[Inject, EntityContext("SalesDb")] IDataSet<Product> _products;
[Inject, EntityContext("ContactsDb")] IDataSet<Person> _people;

Transactions

[Transactional]
public void AddProduct(string name)
{
  /* ... */
}

Audit

public class Person
{
   public string Name {get; set;}

   [CreatedDate]
   public DateTime CreatedAt {get; set;}
   [CreatedBy]
   public FooUser CreatedBy {get; set;}
   [LastModifiedDate]
   public DateTime LastModifiedAt {get; set;}
   [LastModifiedBy]
   public FooUser LastModifiedBy {get; set;}
}

public class LoginProducer
{
   [Producer, Auditor] FooUser GetCurrentUser()
   {
       /* return current user, e.g. from identity, Cormo.Security, or session? */
   }
}