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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:50:37+00:00 2026-05-23T07:50:37+00:00

Sample object… public class Ball { public int Id { get; set; } public

  • 0

Sample object…

public class Ball
{
   public int Id { get; set; }
   public String Name { get; set; }

   public Ball()
   {

   }

   public Ball(int id)
   {
      using (var ctx = new MyContext())
      {
          var theBall = (from b in ctx.Balls
                         where b.Id == id
                         select b).SingleOrDefault();

          //How do i now map this to 'this'?
      }
   }

   public static Ball Load(int id)
   {
      using (var ctx = new MyContext())
      {
          return (from b in ctx.Balls
                  where b.Id == id
                  select b).SingleOrDefault();
      }
   }
}

public void Main()
{
    //Not preferred
    Ball firstBall = Ball.Load(1);

    //Preferred
    Ball secondBall = new Ball(1);
}

If you take a look inside the Public Ball constructor which requires an Id to be passed in, is there any way to map the returned object to this class? Without me doing manual assignment of properties… or do i have to use the Static Load method?

Cheers,
D

  • 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-23T07:50:38+00:00Added an answer on May 23, 2026 at 7:50 am

    No you cannot map it to constructed instance (EF must create instance itself so you will always end with two instances) unless you manually assign each value like from returned instance to current instance but anyway that is wrong implementation because constructor should be dumb and it should not perform any heavy operation like database queries.

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

Sidebar

Related Questions

Say I have a simple object such as class Something { public int SomeInt
Sample One Public _objLock As Object = New Object Public ReadOnly Property MyObjects() As
Given an object like: class M(models.Model): test = models.BooleanField() created_date = models.DateTimeField(auto_now_add=True) Given sample
Here's an sample code, where only the string object is released. NSString *nameOfFile =
I am using mockito and trying to mock a scala object. object Sample {
Below is a sample implementation of overriding Object.Equals() for an entity base class from
Given a string for a object property path, how do I set this property
On object ValueSet, I have apply(pairs : (String, ValueBase)*) and implicit conversions from Int
I have a code sample that gets a SEL from the current object, SEL
I have an object graph serialized to xaml. A rough sample of what it

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.