Kamil
Kamil

Reputation: 13931

Can I put a method inside class that is used as entity in Entity Framework?

I have POCO class that is used by Entity Framework. I'm using one field that is not mapped with database because I wanted it to be calculated when I access some data row.

Can I move data processing to some method in that class and expect that Entity Framework will work fine?

public class SomeClass
{
    public int Id { get; set; }
    public string Variable { get; set; }

    [NotMapped]
    public string VariableProcessed
    {
        get
        {
             return Variable.DoSomethingBlaBla();
        }
        set {}
    }
}

I want to rewrite it like this:

public class SomeClass
{
    public int Id { get; set; }
    public string Variable { get; set; }

    [NotMapped]
    public string VariableInverted
    {
        get
        {
             return ProcessVariable(Variable);
        }
        set {}
    }

    private int ProcessVariable(string variable) 
    { 
        return variable.DoSomethingBlaBla();
    }
}

Upvotes: 1

Views: 291

Answers (1)

Mahmoud Shaaban
Mahmoud Shaaban

Reputation: 122

Yes you can. But you don't need to pass a parameter to that function as long as it works on a variable in your POCO class

Upvotes: 1

Related Questions