I'm new to the Entity Framework and I've followed tutorials online to create my SQL Server database and made several models and a context class to include properties to access them.
This is my account model:
public class Account
{
public int ID { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
This is my context class:
public class DashContext : DbContext
{
public DashContext()
: base(Constants.ConnectionString)
{
this.Configuration.LazyLoadingEnabled = true;
this.Configuration.ProxyCreationEnabled = false;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<DashContext>(null);
}
public DbSet<Account> Accounts { get; set; }
}
This works - when I access the DbSet property I can access all the account entires in my database.
However, I want to create an implmentation of the Account
class that contains more properties than just columns because it has to interact with my program.
So, I tried to do the following:
public class GameAccount : Account
{
public int SomeSpecialProperty { get; set; }
}
However, when I'm using my context class to get the Account
object, I'm not sure how to convert it to GameAccount
. I know I can create a constructor that copies the properties from Account
to GameAccount
, like this:
public class GameAccount
{
public int ID { get; private set; }
public string Username { get; private set; }
public string Password { get; private set; }
public GameAccount(Account model)
{
this.ID = model.ID;
this.Username = model.Username;
this.Password = model.Password;
}
}
...but that seems a bit inefficent to me and I'm sure there's a simpler way.
What do you think?