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

  • Home
  • SEARCH
  • 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 3232894
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:12:51+00:00 2026-05-17T17:12:51+00:00

I’m working on a site and there are two projects in the solution a

  • 0

I’m working on a site and there are two projects in the solution a business logic project and the website project. I understand that I want to keep the entity context out of the web project and only use the business objects the framework creates but I can’t figure out how to save a modified object this way.

Let’s say my entity model created this class:

public class Person //Person entity
{
    Int32 Id {get;set;}
    String Name {get;set;}
    Address Address {get;set;} //Address entity
}

And I created this class to get a specific person:

public static class PersonController
{
    public static Person GetById(int id)
    {
        using (Entities context = new Entities())
        {
            return context.Persons.FirstOrDefault(x => x.Id == id);
        }
    }
}

This allows me to get a person without a context by calling PersonController.GetById(1); and I can change the persons properties after I get them but I can’t figure out how to save the modified information back to the database. Ideally I would like to partial class Person and add a .Save() method which would handle creating a context adding the person to it and saving the changes. But when I tried this a while ago there were all kinds of issues with it still being attached to the old context and even if I detatch it and attatch it to a new context it gets attached as EntityState.Unchanged, if I remember right, so when I call context.SaveChages() after attaching it nothing actually gets updated.

I guess I have two questions:

1) Am I going about this in a good way/is there a better way? If I’m doing this in a really terrible way I would appreciate some psudo-code to point me in the right direction; a link to a post explaining how to go about this type of thing would work just as well.

2) Can someone provide some psudo-code for a save method? The save method would also need to handle if an address was attached or removed.

  • 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-17T17:12:51+00:00Added an answer on May 17, 2026 at 5:12 pm

    There are many ways to handle Entity Framework as a persistence layer.

    For one, it looks like you’re not using pure POCOs. That is, you let EF generate the classes for your (in the EDMX.designer.cs file).

    Nothing wrong with that, but it does inhibit a clean separation of concerns (especially when it comes to unit testing).

    Have you considering implementing the Repository pattern to encapsulate your EF logic? This would be a good way to isolate the logic from your UI.

    In terms of Save – this is where it gets difficult. You’re right, most people use partial classes. Generally, you would have a base class which exposes a virtual “Save” method, which the partial classes can then override.

    I personally don’t like this pattern – i believe POCOs should not care about persistence, or the underlying infrastructure. Therefore I like to use pure POCOs (no code gen), Repository pattern and Unit of Work.

    The Unit of Work handles the context opening/saving/closing for you.

    This is how (my) Unit of Work does the magic. Consider this some code in your “Web” project:

    var uOw = new UnitOfWork(); // this is class i created, implementing the UOW pattern
    var person = repository.Find(10); // find's a "Person" entity (pure POCO), with id 10.
    person.Name = "Scott";
    uOw.Commit();
    

    Or adding a new Person:

    var uOw = new UnitOfWork();
    var newPerson = new Person { Name = "Bob" };
    repository.Add(newPerson);
    uOw.Commit();
    

    How nice is that? 🙂

    Line 1 creates a new sql context for you.
    Line 2 uses that same context to retrieve a single “Person” object, which is a hand-coded POCO (not generated by EF).
    Line 3 changes the name of the Person (pure POCO setter).
    Line 4 Saves the changes to the data context, and closes the context.

    Now, there is a LOT more to these patterns than that, so I suggest you read up on these patterns to see if it suits you.

    My repository is also implemented with Generics, so I can re-use this interface for all business entity persistence.

    Also take a look at some of the other questions I have asked on Stack Overflow – and you can see how I’ve implemented these patterns.

    Not sure if this is the “answer” you’re looking for, but thought I’d give you some alternative options.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.