动态选择哪个表将数据插入到实体框架 [英] Dynamically select which table to insert data into with entity framework

查看:119
本文介绍了动态选择哪个表将数据插入到实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我写这需要在运行时选择一个实体,并通过实体框架添加此保存到数据库的应用程序,我已经通过使用ADO.NET实体的创建基于关闭它的数据库和模型数据模型,所述对象,其中容纳被选择baised上是在运行时被添加与使用反射类的数据的数据,如下:

So I am writing an application which need to select an entity at run time and add through the entity framework save this to a database, I have created the database and a model based off it through the use of an ADO.NET Entity Data model, the objects, which hold the data are chosen baised on the data that is being added at runtime with the use of the reflection class, as follows;

                mytype = Type.GetType(objName);
                myObject = Activator.CreateInstance(mytype);

我怎样才能做这样的事情来选择要使用的实体,是我可以做这样的事情:

How can I do something like this to select the entity to use, is can i do something like this:

                db.[MyEntity].Add(myObject);
                db.SaveChanges();

感谢

推荐答案

您可以得到的 DbSet 通过实体类型:

You can get DbSet by entity type:

var set = db.Set(myObject.GetType());
set.Add(myObject);
db.SaveChanges();

这篇关于动态选择哪个表将数据插入到实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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