从任何Entity对象获取数据库上下文(表) [英] Getting Database Context from any Entity object (tables)

查看:177
本文介绍了从任何Entity对象获取数据库上下文(表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我目前使用Entity framework 6来连接Sql Server数据库。我确实在我的基本实体模型中添加了一些方法,使用了部分定义,并且在网上找到的一些代码获得了DbContext(Cycles是数据库表之一):

使用System.Data.Entity.Core.Objects; 
使用System.Data.Entity;
使用System.Data.Entity.Infrastructure;
使用System.Runtime.InteropServices;

公共部分类实体
{
公共静态DbContext GetDbConext(对象实体)
{
System.Reflection.FieldInfo field = entity.GetType() .GetField(QUOT; _entityWrapper");

if(field == null)返回null;

object wrapper = field.GetValue(entity);
System.Reflection.PropertyInfo property = wrapper.GetType()。GetProperty(" Context");
ObjectContext oc =(ObjectContext)property.GetValue(wrapper,null);
返回新的DbContext(oc,dbContextOwnsObjectContext:false);
}
}
公共部分类周期
{
公共PAProcedures GetProcedure()
{
object o = Entity.GetDbConext(this) ;
MyEntities dbc =(MyEntities)o;
PAProcedures value = dbc.PAProcedures.Where(x => x.IdPere == this.PartEnd.IdPere&& x.Nom == this.Procedur).SingleOrDefault();
返回值;
}
}

这会在 

 MyEntities dbc =(MyEntities)o; 

  System.InvalidCastException未处理

  HResult = -2147467262

  Message =无法将"System.Data.Entity.DbContext"类型的对象强制转换为"MyEntities"。



提前感谢您的支持帮助。


Rgds。


Pascal。

解决方案

Hi Pascal,


根据您的描述,您的案例与实体框架6更相关,我将帮助您将案例移至EF论坛以获得更好的支持。



祝你好运,


Kristin



Hi,

I currently use Entity framework 6  to interface Sql Server database. I did add some methods to my base Entity Model, using partial definition, and some code found on the net get the DbContext (Cycles is one of database table):

using System.Data.Entity.Core.Objects;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Runtime.InteropServices;

    public partial class Entity
    {
        public static DbContext GetDbConext(object entity)
        {
            System.Reflection.FieldInfo field = entity.GetType().GetField("_entityWrapper");

            if (field == null) return null;

            object wrapper = field.GetValue(entity);
            System.Reflection.PropertyInfo property = wrapper.GetType().GetProperty("Context");
            ObjectContext oc = (ObjectContext)property.GetValue(wrapper, null);
            return new DbContext(oc,dbContextOwnsObjectContext: false);
        }
    }
    public partial class Cycles
    {
        public PAProcedures GetProcedure()
        {
            object o = Entity.GetDbConext(this);
            MyEntities dbc = (MyEntities)o;
            PAProcedures value = dbc.PAProcedures.Where(x => x.IdPere == this.PartEnd.IdPere && x.Nom == this.Procedur).SingleOrDefault();
            return value;
        }
    }

And that is generate CastException on 

MyEntities dbc = (MyEntities)o;

 System.InvalidCastException was unhandled
  HResult=-2147467262
  Message=Unable to cast object of type 'System.Data.Entity.DbContext' to type 'MyEntities'.

Thanks in advance for your help.

Rgds.

Pascal.

解决方案

Hi Pascal,

Based on your description, your case more related to Entity framework 6, I will help move your case to EF forum for better support.

Best regards,

Kristin


这篇关于从任何Entity对象获取数据库上下文(表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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