简体   繁体   English

实体框架核心 - 子类值为null

[英]Entity Framework Core - Child class values null

I have one DBSet for all my child classes -> base class is EventModel. 我的所有子类都有一个DBSet - >基类是EventModel。 When i try to load all EventUpdateCartModel (One child class from EventModel) with all datas the child datas always null. 当我尝试使用所有数据加载所有EventUpdateCartModel(来自EventModel的一个子类)时,子数据始终为null。 The base values are available like SettingsModel or AccountModel. 基本值可用于SettingsModel或AccountModel。

** My Question:** ** 我的问题:**

How can i load all datas with the base class DbSet and not with the child class DbSet. 如何使用基类DbSet加载所有数据,而不加载子类DbSet。

Classes

public abstract class EventModel
{

 ... Some Other Datas
 public virtual SettingsModel SettingsModel { get; set; }
 public virtual AccountModel AccountModel { get; set; }
}

public class EventUpdateCartModel : EventModel
{
  public virtual UpdateCartDataModel UpdateCartDatas { get; set; }
}

public class UpdateCartDataModel
{
  public long UpdateCartDataId { get; set; }
  public EventUpdateCartType Action { get; set; }  
}

DbSet DbSet

  public DbSet<EventModel> Events { get; set; }

Read Rows 阅读行

 var events = context.Events.ToList();

空值

When i add a new DbSet for the child class and try to get the value with this DbSet all values are available. 当我为子类添加一个新的DbSet并尝试使用此DbSet获取值时,所有值都可用。

DbSet DbSet

 public DbSet<EventUpdateCartModel> Events_UpdateCart{ get; set; }

Read Rows 阅读行

var events = context.Events_UpdateCart.ToList();

不是空的

** My Question:** ** 我的问题:**

How can i load all datas with the base class DbSet and not with the child class DbSet. 如何使用基类DbSet加载所有数据,而不加载子类DbSet。

I hope somebody can help me. 我希望有人可以帮助我。

One approach would be to change your query to return an anonymous type that only includes the necessary fields from your model and omit the child DbSet. 一种方法是更改​​查询以返回仅包含模型中必需字段的匿名类型,并省略子DbSet。 For example: 例如:

var events = context.Events_UpdateCart
                .Select(u => new {
                      EventId = u.EventId,
                      EventType = u.EventType
                });

Read more on the EF core docs . 阅读有关EF核心文档的更多信息

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

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