So I'm writing a framework for my projects.
This framework should take the PT out of starting new projects by providing a generic foundation upon which the project is built.
The framework includes:
- A generic db context to which is provided by the application a database name
- Generic data models that include "common" data such as a user model with an email address and password property
- Generic CRUD functionality for the generic data models
So far I feel like I've got stuck early on in my attempt to make my DbContext class as generic as possible. See below:
public class DataEntities : DbContext
{
public string _databaseName { get; set; }
public string DatabaseName
{
get
{
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 this code and I've been unable to find a solution to either of them:
On the line : base(DatabaseName)
:
An object reference is required for the non-static field, method, or property 'LYSFramework.DataEntities.DatabaseName.get'
As neither the class nor the constructor are static, I don't understand what it is that I've done wrong here.
On the line var Init;
Implicitly-typed local variables must be initialized
It is implicitly typed because I don't know what type to set it to yet. It should get its type later in the switch
.
My questions are as follows:
- How do I use the string property's value in the base inheritance on the constructor?
- What type should I set
Init
to before I use it? (Ideally it should basically just be null I guess).
Thanks in advance!