Closed mchomem closed 3 years ago
You setup up the dbfactory correctly but you never call it to get the database. In your Get
method of your context change to the following.
return DbFactory.GetDatabase();
Your context class should also be a singleton and be injected rather than newing it up each time.
Hello @schotime .
Very thanks for your help.
I did the updates in my project with your instructions. Now, is using fluent mapping from NPoco.
Strong hug
Hello @schotime
I need a help to correctly use Fluent from NPoco.
I'm not able to use Fluent from NPoco.
Searching by examples and issues too, i don't found something which helps me.
If you have a full tutorial (with source code), will be of big help to my knowledge.
Next, my attempt to use NPoco's fluent mapping:
namespace MCHomem.NPoco.Proto.ConsoleApp { class Program { static void Main(string[] args) { Console.Title = "NPoco CRUD";
}
using MCHomem.NPoco.Proto.Models.Mappings; using NPoco; using NPoco.FluentMappings; using NPoco.SqlServer;
namespace MCHomem.NPoco.Proto.Models.Repositories { public class TestAppContext { public DatabaseFactory DbFactory { get; set; }
}
using System;
namespace MCHomem.NPoco.Proto.Models.Entities { public class Employee { public Int32? EmployeeID { get; set; } public String Name { get; set; } public String DocumentNumber { get; set; } public Boolean? Active { get; set; } public DateTime CreatedIn { get; set; } public DateTime? UpdatedIn { get; set; } } }
using MCHomem.NPoco.Proto.Models.Entities; using NPoco.FluentMappings;
namespace MCHomem.NPoco.Proto.Models.Mappings { public class EmployeeMapping : Map
{
public EmployeeMapping()
{
this.TableName("Employee"); this.PrimaryKey("EmployeeID"); this.Columns(x => { x.Column(c => c.EmployeeID); x.Column(c => c.Name); x.Column(c => c.DocumentNumber); x.Column(c => c.Active); x.Column(c => c.CreatedIn); x.Column(c => c.UpdatedIn); }); } } }
using MCHomem.NPoco.Proto.Models.Entities; using NPoco; using System.Collections.Generic; using System.Text;
namespace MCHomem.NPoco.Proto.Models.Repositories { public class EmployeeRepository : ICrud
{
public void Create(Employee entity)
{
using (IDatabase db = new TestAppContext().Get())
{
db.Insert(entity);
}
}
}