简体   繁体   中英

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".

For this i simply created another partial class corresponding to the Employee class generated in my data model as below

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

But when i query the DB related to Employee it throws following error :

例外!

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. Also, modifying the value variable in the setter doesn't do what you want. 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:

    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. 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. Personally i would have done it that way:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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