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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:33:57+00:00 2026-05-24T22:33:57+00:00

I’m using Entity Framework 4.1. I have a normal model .edmx which maps a

  • 0

I’m using Entity Framework 4.1. I have a normal model .edmx which maps a Match class to a ‘Match’ database table and this can be accessed as normal using EF.

However I require custom properties methods for the Match so I extended this using a partial class and I can add my properties etc.

All of this works fine, however I just can’t find out how to instantiate an instance of my partial match class by its primary key / id. i.e so I can pass the Id into the constructor and have the object populated with all of its data from the database.

I know we can do the following to populate from calling code:

 public Match PopulateforMatchId(int matchId)
    {
        var match = (from m in _db.Matches
                         .Include("TeamA")
                         .Include("TeamB")
                         .Include("Season")
                         .Include("Season.Competition")
                     where m.Match_ID == matchId
                     select m).FirstOrDefault();

        return match;

    }

However this is not what I need as this is not self contained within the partial class itself, I need it to populate itself, as other properties in the partial class rely on the object itself having its data in place before they can be calculated.

Anyone have any ideas how i can do this?

Thanks

Kevin

  • 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-24T22:33:59+00:00Added an answer on May 24, 2026 at 10:33 pm

    This is wrong way to use Entity framework. Entity framework is not suitable for easy populating existing object. Moreover it demands that entity has internal dependency on the EF context.

    How to probably make it work (but I definitely not recommend it):

    public Match(int matchId) 
    {
        // You called constructor yourselves = you have unproxied entity without
        // dynamic change tracking and without lazy loading
    
        Id = matchId;
    
        // I'm not sure if this is possible in entity constructor but generally it should work
    
        // Get context somewhere - service locator pattern
        ObjectContext context = ContextProvider.GetCurrent();
        context.Matches.Attach(this);
        context.Refresh(RefreshMode.StoreWins, this);
        // Now you should have populated Match entity itself but not its navigation properties
    
        // To populate relations you must call separate query for each of them because `Include`
        // is possible only when the entity is created and loaded by EF and lazy loading is off
        // in this case
    
        context.LoadProperty(this, m => m.TeamA);
        context.LoadProperty(this, m => m.TeamB);
    
        Season = (from s in context.Seasons.Include("Competition")
                  select s).ToList();      
    }
    

    This is also the example of wrong constructor – constructor should not take such heavy logic. It should be responsibility of some other initialization method.

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

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.