Entity Framework 4.1 - how to update, insert and delete data in derived classes

烈酒焚心 提交于 2019-12-11 12:19:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!