EF 4继承的类如何查询我的子类

发布于 2024-10-05 01:28:21 字数 3507 浏览 3 评论 0原文

如何在 EF 4 中获得我的子类的 IEnumerable 结果。就像,我的基类有一个 ObjectSet,还有一个 EntitySet ..但是对于我的孩子,我无法查询它们..Whit 有些尝试出现错误,我成功了读取、更新、创建、删除..但不查询(或列出)我的子类..

我这样读:

 public Member GetMember(Guid id)
    {
        尝试
        {
            使用(EntitiesContainer db = new EntitiesContainer())
            {
                返回 db.Resources.SingleOrDefault(x => x.Id == id) 作为成员;
            }
        }
        catch(异常前)
        {
            日志条目 = new Log();
            条目.ClassName = GetType().Name;
            条目.消息=前.消息;
            Entry.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
            条目.StackTrace = ex.StackTrace;
            _logger.Log(条目, SeverityEnum.Critical);
        }

        返回新成员();
    }

我这样创建:

 public Member CreateMember(Member member, string userName)
    {
        尝试
        {
            使用(EntitiesContainer db = new EntitiesContainer())
            {
                member.Id = (Guid)_userService.GetUser(userName).ProviderUserKey;

                db.Resources.AddObject(成员);

                db.SaveChanges();
            }
        }
        catch(异常前)
        {
            日志条目 = new Log();
            条目.ClassName = GetType().Name;
            条目.消息=前.消息;
            Entry.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
            条目.StackTrace = ex.StackTrace;
            _logger.Log(条目, SeverityEnum.Critical);
        }

        返回会员;
    }

我这样更新:

 public Member UpdateMember(会员成员)
    {
        尝试
        {
            使用(EntitiesContainer db = new EntitiesContainer())
            {
                db.Resources.Attach(GetMember(member.Id));

                db.Resources.ApplyCurrentValues(成员);

                db.SaveChanges();
            }
        }
        catch(异常前)
        {
            日志条目 = new Log();
            条目.ClassName = GetType().Name;
            条目.消息=前.消息;
            Entry.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
            条目.StackTrace = ex.StackTrace;
            _logger.Log(条目, SeverityEnum.Critical);
        }

        返回会员;
    }

等等...

但是对于列表或查询我的成员集合或对象集...我无法做到这一点。

我尝试类似的操作..但它返回 null 或强制转换异常。

 public IEnumerable;获取全部()
   {
       尝试
       {
           使用(EntitiesContainer db = new EntitiesContainer())
           {
               var test = db.Resources.ToList() as IList;

               // 或者

               var test = db.Resources as IEnumerable;;

               // 或者

               var test = db.Resources.Cast();

               // 或者

               var test = db.Resources.AsEnumerable();    

               返回测试;
           }
       }
       catch (System.Exception 前)
       {
           日志条目 = new Log();
           条目.ClassName = GetType().Name;
           条目.消息=前.消息;
           Entry.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
           条目.StackTrace = ex.StackTrace;
           _logger.Log(条目, SeverityEnum.Critical);
       }

       返回空值;
   }

我也尝试为我的成员创建一个对象集,但他们告诉我我没有这个对象集的任何实体集..它在逻辑上是正确的..

但我在调试中看到有一个我的成员的 DynamicProxies 实例..属性是正确的,具有良好的价值..但我不知道如何达到它..我可以相信EF团队他们没有考虑到这一点...我在网上到处搜索..没有成功..我需要一个专家:)!

非常感谢您抽出时间!我很欣赏!

诚挚地, 朱利安.

How to have an IEnumerable result of my child class in EF 4. Like, I have an ObjectSet for my base class an EntitySet too .. but for my child I have no way to query them .. Whit some try an error I succeed to Read, Update, Create, Delete .. but not to query (or list) my child class..

I Read like this :

    public Member GetMember(Guid id)
    {
        try
        {
            using (EntitiesContainer db = new EntitiesContainer())
            {
                return db.Resources.SingleOrDefault(x => x.Id == id) as Member;
            }
        }
        catch (Exception ex)
        {
            Log entry = new Log();
            entry.ClassName = GetType().Name;
            entry.Message = ex.Message;
            entry.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
            entry.StackTrace = ex.StackTrace;
            _logger.Log(entry, SeverityEnum.Critical);
        }

        return new Member();
    }

I Create like this :

    public Member CreateMember(Member member, string userName)
    {
        try
        {
            using (EntitiesContainer db = new EntitiesContainer())
            {
                member.Id = (Guid)_userService.GetUser(userName).ProviderUserKey;

                db.Resources.AddObject(member);

                db.SaveChanges();
            }
        }
        catch (Exception ex)
        {
            Log entry = new Log();
            entry.ClassName = GetType().Name;
            entry.Message = ex.Message;
            entry.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
            entry.StackTrace = ex.StackTrace;
            _logger.Log(entry, SeverityEnum.Critical);
        }

        return member;
    }

I Update like this :

    public Member UpdateMember(Member member)
    {
        try
        {
            using (EntitiesContainer db = new EntitiesContainer())
            {
                db.Resources.Attach(GetMember(member.Id));

                db.Resources.ApplyCurrentValues(member);

                db.SaveChanges();
            }
        }
        catch (Exception ex)
        {
            Log entry = new Log();
            entry.ClassName = GetType().Name;
            entry.Message = ex.Message;
            entry.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
            entry.StackTrace = ex.StackTrace;
            _logger.Log(entry, SeverityEnum.Critical);
        }

        return member;
    }

And so on...

But for list or query my Members Collection or ObjectSet ... I have no way for doing this.

I try something like that .. but it return me null or a casting exception.

   public IEnumerable<Member> GetAll()
   {
       try
       {
           using (EntitiesContainer db = new EntitiesContainer())
           {
               var test = db.Resources.ToList() as IList<Member>;

               // OR

               var test = db.Resources as IEnumerable<Member>;

               // OR

               var test = db.Resources.Cast<Member>();

               // OR

               var test = db.Resources.AsEnumerable<Member>();    

               return test;
           }
       }
       catch (System.Exception ex)
       {
           Log entry = new Log();
           entry.ClassName = GetType().Name;
           entry.Message = ex.Message;
           entry.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
           entry.StackTrace = ex.StackTrace;
           _logger.Log(entry, SeverityEnum.Critical);
       }

       return null;
   }

I Try to create an ObjectSet for my Member too but they throw me that i dont have any EntitySet for this ObjectSet .. that its logicly right ..

But I see in debugging that there an instance of DynamicProxies of the my Member .. an this properties is right with the good value .. but i dont know how to reach it .. I can believe that EF team they have not think for that ... I search everywhere in the net .. no sucess .. I need an expert :) !

Thank you a lot for your time! I appreciate!

Cordialy,
Julien.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满天都是小星星 2024-10-12 01:28:21
EntitiesContainer db = new EntitiesContainer();
var members = db.Resources.OfType<Member>();
EntitiesContainer db = new EntitiesContainer();
var members = db.Resources.OfType<Member>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文