本文介绍了在实体框架,我怎么一个通用的实体添加到其对应的DbSet不枚举所有可能DbSets一个switch语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有两个类型的实体:雇员实体和一个办公室的实体,具有一对为一个办公室的两个这样的,有很多雇员之间一对多关系。对于EF,一个DbSet在每个实体的背景文件中创建: 公共DbSet<办公室及GT;办公室{搞定;组; } 公共DbSet<员工>员工{搞定;组; } 这是EF教程,我也有我做我的CRUD方法为特定的实体。例如,下面的方法创建一个办公室,并将其添加到办公室DbSet(忽略MVC的东西 - 我不这样做了): 公众的ActionResult创建([绑定(包括=地址,BUSINESSNAME)]处办公室) {试 { 如果(的ModelState .IsValid) { db.Offices.Add(办公室); db.SaveChanges(); 返回RedirectToAction(「指数」); } } 赶上(DataException / * * DEX /) { //登录错误(DEX取消注释变量名,并添加一行这里写日志 ModelState.AddModelError(,无法保存更改再试一次,如果问题仍然存在,请与系统管理员); } 返回查看(办公室); } 基本上,两件事我想强调的是,一个办公室对象被传递到方法,并且该办公室被明确地写入db.Offices添加到Office DbSet: 分贝。 Offices.Add(办公室); 不过,我需要写一个方法,其中的通用实体对象可以传递的,我可以将它添加到其正确DbSet因为我有方法的粗略的想法是这样的(我忽略了所有的MVC的东西): 公共无效创建(对象entityToCreate) { db.CorrespondingEntityType.Add(entityToCreate); db.SaveChanges(); } 所以我们可以说我有一个Employee对象。我可以通过这个员工将在创建方法,它可以看到,这是一个员工,所以这将它添加到员工DbSet。我不知道如果EF支持这虽然。另一种方法是做一个switch语句,并根据不同的实体类型的方式传递中,我可以直接调用哪个DbSet添加实体。但我想避免,因为我将不仅仅是这两个有更多的实体合作。此外,我将不得不对其他CRUD方法做类似的事情。 我看到的从MSDN本文档有关ObjectSet.AddObject方法,似乎像它应该是有用的,但我不知道它是如何工作的。 解决方案 您可以考虑像这样一个泛型类: 公开类GenericRepository< T> :其中T:类 {内部YourConext背景; 内部DbSet< T> dbSet; 公共GenericRepository(YourContext上下文) { this.context =背景; this.dbSet = context.Set< T>(); } 公共虚拟无效插入(T实体) { dbSet.Add(实体); context.SaveChanges(); } } I have two types of entities: an employee entity and an office entity, with a one to many relationship between the two such that there are many employees for one office. For EF, a DbSet is created in the context file for each entity:public DbSet<Office> Offices { get; set; }public DbSet<Employee> Employees { get; set; }An EF tutorial that I did had me do my CRUD methods for a specific entity. For example, the method below creates an office and adds it to the office DbSet (ignore the MVC stuff -- I am not doing that anymore):public ActionResult Create([Bind(Include = "Address,BusinessName")] Office office) { try { if (ModelState.IsValid) { db.Offices.Add(office); db.SaveChanges(); return RedirectToAction("Index"); } } catch (DataException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } return View(office); }Basically the two things I want to emphasize is that an Office object is passed into the method, and that the office is added to the Office DbSet by explicitly writing db.Offices:db.Offices.Add(office);However, I need to write a method in which a generic entity object can be passed in, and I can add this to its correct DbSet. The rough idea for the method I have is something like this (I have ignored all the MVC stuff):public void Create(object entityToCreate){ db.CorrespondingEntityType.Add(entityToCreate); db.SaveChanges();}So let's say I have an Employee object. I can pass this Employee into the Create method and it can see that this is an Employee, and so it would add it to the Employees DbSet. I don't know if EF supports this though. An alternative would be to make a switch statement and that way depending on the type of the entity being passed in, I could directly call which DbSet to add the entity to. But I want to avoid that because I will be working with a lot more entities than just these two. Also I will be having to do similar things for the other CRUD methods. I saw this documentation from msdn about the ObjectSet.AddObject Method, and it seems like it should be useful, but I'm not sure how it works. 解决方案 You might consider a generic class like so: public class GenericRepository<T> : where T : class { internal YourConext context; internal DbSet<T> dbSet; public GenericRepository(YourContext context) { this.context = context; this.dbSet = context.Set<T>(); } public virtual void Insert(T entity) { dbSet.Add(entity); context.SaveChanges(); } } 这篇关于在实体框架,我怎么一个通用的实体添加到其对应的DbSet不枚举所有可能DbSets一个switch语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 10:49