c# - My first attempt at an application framework using Entity Framework -
so i'm writing framework projects.
this framework should take pt out of starting new projects providing generic foundation upon project built.
the framework includes:
- a generic db context provided application database name
- generic data models include "common" data such user model email address , password property
- generic crud functionality generic data models
so far feel i've got stuck on in attempt make dbcontext class generic possible. see below:
public class dataentities : dbcontext { public string _databasename { get; set; } public string databasename { { if (string.compare(_databasename, string.empty, stringcomparison.invariantcultureignorecase) == 0) { return "lysdatabase"; } else { return _databasename; } } set; } public initializer _initializer { get; set; } public enum initializer { dropcreatedatabasealways = 1, dropcreatedatabaseifmodelchanges = 2, createdatabaseifnotexists = 3 } public dataentities() : base(databasename) { var init; switch (_initializer) { case initializer.createdatabaseifnotexists: init = new createdatabaseifnotexists<dataentities>(); break; case initializer.dropcreatedatabasealways: init = new dropcreatedatabasealways<dataentities>(); break; case initializer.dropcreatedatabaseifmodelchanges: init = new dropcreatedatabaseifmodelchanges<dataentities>(); break; } database.setinitializer<dataentities>(init); } }
i have 2 errors in code , i've been unable find solution either of them:
on line : base(databasename)
:
an object reference required non-static field, method, or property 'lysframework.dataentities.databasename.get'
as neither class nor constructor static, don't understand i've done wrong here.
on line var init;
implicitly-typed local variables must initialized
it implicitly typed because don't know type set yet. should type later in switch
.
my questions follows:
- how use string property's value in base inheritance on constructor?
- what type should set
init
before use it? (ideally should null guess).
thanks in advance!
how use string property's value in base inheritance on constructor?
when dataentities
constructor called, first thing tries call dbcontext
(base class) constructor. @ point, don't have complete instance of dataentities
class, can't pass instance property databasename
base constructor. you'll need modify dataentities
constructor take database parameter can pass on base class constructor.
what type should set
init
before use it?
once init
declared, type cannot changed. var
can used declare variable if initialized compiler can tell type is.
you hae declare init
type compatible want assign later. few ways:
[recommended] create interface
idatabaseinitializer
each of types implements, , change declarationidatabaseinitializer init;
declare
init
object
, since every class in c# implicitly inheritsobject
.use
dynamic
keyword (not recommended - leads fragile code).
Comments
Post a Comment