i'm testing ef codefirst ctp5 , trying implement unit of work , repository patterns. when run simple test get:
system.invalidoperationexception : entity type log not part of model current context.
the database get's created ef , fails when call .add() seems same context not being used can't figure out why?
hoping smart human coming rescue me!
in advance taking time.
here code:
logcabincontext
public class logcabincontext : dbcontext, iunitofwork { public logcabincontext() { } public logcabincontext(string nameorconnectionstring) : base(nameorconnectionstring) { } #region iunitofwork members public void save() { base.savechanges(); } #endregion }
baserepository
public class baserepository<t> : ibaserepository<t> t : entitybase { public logcabincontext _unitofwork; private dbset<t> _dbset; public baserepository(iunitofwork unitofwork) { if (unitofwork == null) throw new nullreferenceexception("unitofwork must not null"); _unitofwork = unitofwork logcabincontext; _dbset = _unitofwork.set<t>(); } #region ibaserepository members public t getbyid(int id) { return _dbset.singleordefault(x => x.id == id); } public void add(t entity) { _dbset.add(entity); } public void delete(t entity) { _dbset.remove(entity); } public ienumerable<t> list() { return _dbset.orderby(x => x.id).asenumerable(); } public iunitofwork currentunitofwork { { return _unitofwork; } } #endregion #region idisposable members public void dispose() { _unitofwork.dispose(); } #endregion }
simpletest, using ninject build context, works since creates db
[testfixture] public class logrepositorytests { ikernel _kernel; [setup] public void setup() { _kernel = new standardkernel(new databasemodule()); } [teardown] public void teardown() { _kernel.dispose(); } public ilogrepository getlogrepository() { ilogrepository logrepo = _kernel.get<ilogrepository>(); return logrepo; } [test] public void test() { using (ilogrepository repo = this.getlogrepository()) { log mylog = new log(); mylog.application = "test"; mylog.date = datetime.now; mylog.exception = "exception message"; mylog.machinename = "local"; mylog.message = "testing"; mylog.stacktrace = "stacktrace"; repo.add(mylog); } } }
ilogrepository, derives base now
public interface ilogrepository : ibaserepository<log> { }
Comments
Post a Comment