1

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?

Gilbert Williams
  • 175
  • 1
  • 10
  • Create another property in the Context like `public DbSet GameAccounts {get;set;}` and there you'll have all those rows. – dcg Apr 08 '17 at 17:29
  • @dcg This is incorrect. The Entity Framework will attempt to query to a table called "GameAccount". – Gilbert Williams Apr 08 '17 at 17:33

2 Answers2

2

You have a few options:

Option 1

Use a partial class as indicated by Fruchtzwerg.

Option 2

You can use AutoMapper to map the items from one type to the other. Here is an example:

// Notice the ReverseMap call. That will allow mapping in both directions.
Mapper.Initialize(cfg => 
    cfg.CreateMap<Account, GameAccount>().ReverseMap());
var account = Mapper.Map<Account>(new GameAccount());
var gameAccount = Mapper.Map<GameAccount>(account);
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
1

Copy Constructors could be very costly to develop and maintenance. Typically generated classes of the Entity Framework are partial.

BradleyDotNET explains:

When code is generated; you don't want your additional methods/properties/whatever blown away, so the designers mark such classes partial to allow users to put additional code in a different file.

So a possible approach is extending the class

public partial class Account
{
    public int ID { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
}

with additional properties like

public partial class Account
{
    public int SomeSpecialProperty { get; set; }
}
Community
  • 1
  • 1
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
  • You are almost right. But there is no code generation in this case because it is code first approach. – CodingYoshi Apr 08 '17 at 17:33
  • @CodingYoshi Yes, but he was just giving the example from the generated code. This is probably my solution - but it seems a bit weird having my classes separate and still specifing same namespace. Can't I use different namespaces? – Gilbert Williams Apr 08 '17 at 17:34
  • @GilbertWilliams No, a partial class have to be written in the same namespace (have a look at http://stackoverflow.com/questions/4504288/partial-class-in-different-namespaces) – Fruchtzwerg Apr 08 '17 at 17:36