4

I have the following BaseballDbContext class:

public class BaseballDbContext : DbContext
{
   public DbSet<BaseballTeam> teams { get; set; }

   protected override void OnModelCreating(ModelBuilder modelBuilder)
   {
       modelBuilder.Entity<Hitter>();
       modelBuilder.Entity<Pitcher>();
   }
}

And my model classes are:

public class BaseballTeam
{
    public int Id { get; set; }
    public string teamName { get; set; }
    public List<BaseballPlayer> players { get; set; }
}

public abstract class BaseballPlayer
{
    public int Id { get; set; }
    public int age { get; set; }
    public string name { get; set; }
}

public class Hitter : BaseballPlayer
{
    public int homeruns { get; set; }
}

public class Pitcher : BaseballPlayer
{
    public int strikeouts { get; set; }
}

Initially seeded data in the players table:

enter image description here

Now I want to update name and homeruns property of one of the hitters:

BaseballTeam team = _ctx.teams.Include(q => q.players).FirstOrDefault();
Hitter hitter = team.players.OfType<Hitter>().FirstOrDefault();
hitter.name = "Tulowitzki";  //that property will be updated
hitter.homeruns = 399;       //but that will not :(

int i = team.players.FindIndex(q => q.Id == hitter.Id);
team.players[i] = hitter;

_ctx.Update(team);
_ctx.SaveChanges();

After I run the code only player's name got update, but not the homeruns property:

enter image description here

How to update property of both child and parent class ?

5
  • Don't you need a DbSet<BaseballPlayer> too? See here Commented Apr 7, 2016 at 22:48
  • No, I do not need it. It is an abstract class. Commented Apr 7, 2016 at 22:48
  • This might be worth a shot: _ctx.Entry(team).State = EntityState.Modified put it before the update call. Commented Apr 7, 2016 at 23:27
  • Tried, and unfortunately it does not solve the problem. Commented Apr 7, 2016 at 23:31
  • As I understand that issue is already fixed, and will be available with next release github.com/aspnet/EntityFramework/commit/… Commented Apr 8, 2016 at 17:37

1 Answer 1

2

From this answer but this is a workaround: Save changes to child class properties using base class query with Entity Framework TPH patten :

Do Not track changes using AsNoTracking()

using (var context = new BaseballDbContext())
{
    var team = context.teams.Include(q => q.players).AsNoTracking().FirstOrDefault();
    var hitter = team.players.OfType<Hitter>().FirstOrDefault();
    hitter.name = "Donaldson";
    hitter.homeruns = 999;
    context.Update(team);
    context.SaveChanges();
}

I think you should have a look at the opened issues related to inheritance and may be open a new issue

Sign up to request clarification or add additional context in comments.

1 Comment

Sure... That s why maybe you have to open an issue... I think because you are not tracking the changes, when you perform the update the context verify every property on every object but really not sure

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.