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 694245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:50:13+00:00 2026-05-14T02:50:13+00:00

I’m working with a small model in Entity Framework 4.0. I’d like to have

  • 0

I’m working with a small model in Entity Framework 4.0. I’d like to have an instance method of the object that represents an entity persist the entity to the database. So instead of from "external" code:

public static void Main(string[] args)
{
   using (EFContext ctx = new EFContext())
   {
       context.AnEntitySet.AddObject(refToTheEntityInstance);
       context.SaveChanges();

Instead, I want the instance of the entity to persist itself, where Contact is the entity name.

public ContactInstance : Contact
{
    public void Persist(List<AnotherEntity> otherEntityList)
    {
        using (EFContext ctx = new EFContext())
        {
            ...
            ctx.Contacts.AddObject(this); // DOESN'T WORK.
            ...
            ...wire up navigation property to collection of AnotherEntity...
            ctx.SaveChanges();

I’m doing something wrong. Is this a bad design? It seems to me that an entity, like any object in object oriented design, should "know" how to persist itself.

  • 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-14T02:50:13+00:00Added an answer on May 14, 2026 at 2:50 am

    From a patterns perspective you are trying to introduce the ActiveRecord pattern, which some people love, some people hate. So asking if this is bad design might turn religious very quickly 🙂

    Having said that it is a common pattern, unfortunately it is not natively supported by the EF.

    There are a number of problems with your code:

    1) ContactInstance can’t be treated as a Contact, which is what you appear to be trying to do, in EF, if you have a derived type in the CLR (i.e. ContactInstance) it must correspond to a derived type in the Entity Model too. (i.e. an Entity type called ContactInstance) which I suspect you don’t have. I’m guess you have this just to add the Persist method. Another way to do that is in a partial class (EF works fine with partial classes:

    public partial class Contact
    {
        public void Persist(...){}
    }
    

    2) Next your code has some issues with Entities potentially being attached to multiple ObjectContexts, for example if you write this code:

    Contact c = new Contact();
    c.Firstname = ...;
    c.Surname = ...;
    c.Persist();
    c.Surname = ...;
    c.Persist();
    

    It will fail – in the second call to Persist() – because an Entity can only be attached to one Context at a time:

    • The first Persist(). adds the entity to one context.
    • And the second Persist() will attempt to add the same entity to another context. Exception time!.

    The workaround for this is to have an Ambient context somehow, using something like ThreadBound statics or something, but then you have to deal with all sorts of tricky issues.

    Anyway the moral of the story is that what you are trying is possible using EF, but it isn’t easy, you have to really think through things like ObjectContext lifetime, issues like attach / detach etc.

    Hope this helps

    Alex James

    Former EF team member

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