实体框架4.1 - 如何更新,插入和删除派生类中的数据 [英] Entity Framework 4.1 - how to update, insert and delete data in derived classes

查看:181
本文介绍了实体框架4.1 - 如何更新,插入和删除派生类中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我如何从UserProfile中插入,更新或删除数据?

我有一个名为UserProfile的类,

解决方案

您将在上下文中定义 DbSet 。您可以定义一组基本的用户类型,它将能够使用 User 和所有派生实体类型。 / p>

  public class上下文:DbContext 
{
public DbSet< User>用户{get;组;
}

使用它与其他任何一样。



插入:

  context.Users.Add(new UserProfile(){...} ); 

修改:

 code> var profile = GetSomeProfile(); 
context.Entry(profile).State = EntityState.Modified;

删除:

 code> var anotherProfiele = GetSomeOtherProfile(); 
context.Users.Remove(anotherProfile);


I have a class called UserProfile, that derives from an object called User.

How do I insert, update or delete data from the UserProfile?

解决方案

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);

这篇关于实体框架4.1 - 如何更新,插入和删除派生类中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆