简体   繁体   English

实体框架-从模型继承

[英]Entity Framework - inheriting from model

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. 我是Entity Framework的新手,我在线上跟随了教程来创建我的SQL Server数据库,并制作了几个模型和一个上下文类来包括访问它们的属性。

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. 这有效-当我访问DbSet属性时,我可以访问数据库中的所有帐户整体。

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. 但是,我想创建一个Account类的实现,该类包含比列更多的属性,因为它必须与我的程序进行交互。

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 . 但是,当我使用上下文类获取Account对象时,我不确定如何将其转换为GameAccount I know I can create a constructor that copies the properties from Account to GameAccount , like this: 知道我可以创建一个构造函数,将属性从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;
    }
}

...but that seems a bit inefficent to me and I'm sure there's a simpler way. ...但是对我来说这似乎效率不高,我敢肯定有一种更简单的方法。

What do you think? 你怎么看?

You have a few options: 您有几种选择:

Option 1 选项1

Use a partial class as indicated by Fruchtzwerg. 使用Fruchtzwerg指示的partial类。

Option 2 选项2

You can use AutoMapper to map the items from one type to the other. 您可以使用AutoMapper将项目从一种类型映射到另一种类型。 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);

Copy Constructors could be very costly to develop and maintenance. 复制构造函数的开发和维护成本可能非常高。 Typically generated classes of the Entity Framework are partial. 通常,实体框架生成的类是局部的。

BradleyDotNET explains: BradleyDotNET解释:

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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM