简体   繁体   中英

Entity Framework Core - Child class values null

I have one DBSet for all my child classes -> base class is EventModel. When i try to load all EventUpdateCartModel (One child class from EventModel) with all datas the child datas always null. The base values are available like SettingsModel or AccountModel.

** My Question:**

How can i load all datas with the base class DbSet and not with the child class 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

  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

 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.

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. For example:

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

Read more on the EF core docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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