简体   繁体   English

通过部分类,堆栈溢出异常在实体框架中扩展实体

[英]Extending an entity in entity framework through partial class, stackoverflow exception

trivial mistakes are often most hard to locate ! 琐碎的错误通常最难发现! done this many times but don't know why its throwing his error 做了很多次,但不知道为什么会抛出错误

I need to get employee firstname and lastname concatenated in a property FullName.So that the display member of a combobox can be set to "FullName". 我需要在属性FullName中将雇员的姓和名串联在一起,以便组合框的显示成员可以设置为“ FullName”。

For this i simply created another partial class corresponding to the Employee class generated in my data model as below 为此,我仅创建了另一个与我的数据模型中生成的Employee类相对应的局部类,如下所示

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;

namespace IGarage.DAL
{
    public partial class Employee : EntityObject
    {
        public string FullName
        {
            get
            {
                return FullName;
            }
            set
            {
                value = this.FirstName + " " + this.LastName;
            }
        }
    }
}

in a file named Employee.cs 在名为Employee.cs的文件中

But when i query the DB related to Employee it throws following error : 但是,当我查询与Employee相关的数据库时,会引发以下错误:

例外!

Also when i explore the problem i see this 另外,当我探索问题时,我看到了

这个 ! please advice. 请指教。

As stated in the comments, your implementation doesn't work because the getter of the FullName property just calls itself again, which causes the infinite recursion. 如评论中所述,您的实现无法正常工作,因为FullName属性的getter只会再次调用自身,这将导致无限递归。 Also, modifying the value variable in the setter doesn't do what you want. 另外,在设置器中修改value变量并不能满足您的要求。 It doesn't store the string anywhere (except the local variable which goes out of scope at the end of the setter). 它不将字符串存储在任何地方(局部变量在设置器的末尾超出范围的除外)。

From what I can tell, you want a read-only property with no setter at all: 据我所知,您想要一个完全没有setter的只读属性:

    public string FullName
    {
        get
        {
            return this.FirstName + " " + this.LastName;
        }
    }
public string FullName
    {
        get
        {
            return FullName;
        }
}

here you call the getter of the property, which getter you are implementing, which causes infinite recursion. 在这里,您将调用属性的getter,您正在实现的getter会导致无限递归。 Btw. 顺便说一句。 ask yourself if you really need a setter on this property as the FullName is a concat of a first name and a last name. 问问自己是否真的需要此属性的设置方法,因为FullName是名字和姓氏的结合体。 Personally i would have done it that way: 就我个人而言,我会那样做:

  public string FullName
    {
        get
        {
            this.FirstName + " " + this.LastName;
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM