继承我是新来的实体框架和我在网上跟着教程创建我的SQL Server数据库,并提出了一些模型和上下文类,包括属性来访问它们。实体框架 - 从模型
这是我的个人户口模式:
public class Account
{
public int ID { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
这是我的上下文类:
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; }
}
这工作 - 当我访问DbSet财产我可以访问所有的帐户entires在我的数据库。
但是,我想创建一个Account
类的实现,它包含的属性不仅仅是列,因为它必须与我的程序进行交互。
所以,我试图做到以下几点:
public class GameAccount : Account
{
public int SomeSpecialProperty { get; set; }
}
然而,当我使用我的上下文类来获取Account
对象,我不知道如何将它转换为GameAccount
。我知道我可以创建一个构造函数,复制特性从Account
到GameAccount
,像这样:
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;
}
}
...但似乎有点inefficent给我,我敢肯定有一个简单的方法。
您认为如何?
创建像'公共DbSet游戏帐户的上下文中的另一个属性{获取;集;}'有你有所有这些行。 –
dcg
@dcg这是不正确的。实体框架将尝试查询名为“GameAccount”的表。 –