问题
I have a class called UserProfile, that derives from an object called User.
How do I insert, update or delete data from the UserProfile?
回答1:
You will define DbSet in your context. You can define set of base User type and it will be able to work with User and all derived entity types.
public class Context : DbContext
{
public DbSet<User> Users { get; set; }
}
And using this is same as any other.
Inserting:
context.Users.Add(new UserProfile() { ... });
Modifying:
var profile = GetSomeProfile();
context.Entry(profile).State = EntityState.Modified;
Deleting:
var anotherProfiele = GetSomeOtherProfile();
context.Users.Remove(anotherProfile);
来源:https://stackoverflow.com/questions/6580203/entity-framework-4-1-how-to-update-insert-and-delete-data-in-derived-classes