Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7587301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:38:22+00:00 2026-05-30T19:38:22+00:00

I am starting to know and work with Entity Framework. And I am trying

  • 0

I am starting to know and work with Entity Framework. And I am trying to do some simple spikes to know how it works.

My first question is what is the right way to intercept values from the client UI and change it before persist them.

In example we have this Person model automatically generated by EF 4.x DbContext Generator template:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace EFModelDatabaseFirst
{
    public partial class Person
    {
        #region Primitive Properties        
        public virtual int Id { get; set; }        
        public virtual string FullName { get; set; }       
        public virtual string LastName { get; set; }
        #endregion
    }
}

I would like to modify the FullName setter to always set it in UpperCase. For example in this way:

public partial class Person
{
    private string fullname;

    #region Primitive Properties        
    public virtual int Id { get; set; }        
    public virtual string FullName { 
        get { return fullname; } 
        set { fullname = value.ToUpper(); }       
    public virtual string LastName { get; set; }
    #endregion
}

But I must to continue working on the model and when I do the ‘Add Code Generation Item…’ again from that modified model all my hand written modifications will be lost when the template rewrite all files.

What is the best approach to implement this ?

Thanks.

edit:

=== ANSWER FROM @Codeulike ===

if I try to create a partial class in this way:

public partial class Person
{
    partial void OnFullNameChanging(string value)
    {
        this.FullName = value.ToUpper();
    }
}

Then I have an Infinite loop and so a stackoverflow exception. And I must to delete POCO Classes and activate the model.edmx with Code Generation Strategy: Default

Generating again POCO Classes and trying to create a partial class in this another way:

public partial class Person
{
    private string fullname;

    partial string FullName
    {
        get
        {
            return fullname;
        }
        set
        {
            fullname = value.ToUpper();
        }
    }
}

The compiler gives me an error, because Class Person already contains a definition for ‘FullName’.

The only way to get it to run fine is deleting property on POCO Class Person. But if I re-generate again the POCO Classes all changes are lost.

edit:

=== ANSWER FROM @Codeulike + @Guvante addings ===

At last I have created a partial class in this way:

public partial class Person
{
    partial void OnFullNameChanging(string value)
    {
        // _FullName is the backend field EF generates.
        this._FullName = value.ToUpper();
    }
}

but this doesn’t run and FullName value (on database) is not changed to UpperCase. I have had to do in this another way and it runs fine:

public partial class Person
{
    partial void OnFullNameChanged()
    {
        this._FullName = this.FullName.ToUpper();
    }
}

Regards.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:38:24+00:00Added an answer on May 30, 2026 at 7:38 pm

    The generated Entity classes are Partial classes.

    You can create your bits in separate .cs files, and it will all get compiled together. This lets you add stuff without getting it overwritten by the code generator.

    See here for further details.

    edit: ok, I see your problem with getters and setters. Apparently this is the way to do it:

    How to: Execute Business Logic During Scalar Property Changes
    http://msdn.microsoft.com/en-us/library/cc716747.aspx

    There are some partial methods for each property that you can write code for. You just have to implement the appropriate partial method in your partial class and it will get called.

    e.g.

    partial void OnFullNameChanged()
    {
        // this partial method gets called when the FullName property has changed
    }
    

    edit: Entity framework uses private fields inside the public properties to hold values. They are typically named _<property name> – check your generated code to see what its using. So the code to capitalise FullName would look something like:

    partial void OnFullNameChanged()
    {
        _FullName = _FullName.ToUpper();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm starting to work with Entity Framework and i would like to know how
I don't know if my question will be clear or not, but i'm starting
I'm just starting with Entity Framework and there's a thing I don't understand: They
i'm starting to work with php basics and i have some problem understanding how
I wanted to know what's the best approach to take on, when starting work
Does somebody know how to recover a never-starting eclipse when the error org.eclipse.swt.SWTError: Item
I am starting with c++ and need to know, what should be the approach
I am starting using logback and I want to know if there are better
I'm starting in the Rails world and i want to know how to put
As we know UML contains 13 types of diagrams (structural and behavioral) before starting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.