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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:46:30+00:00 2026-05-20T20:46:30+00:00

Lets say I have: A username entity, with a Int64 as the ID and

  • 0

Lets say I have:

A username entity, with a Int64 as the ID and a few other fields.

A message entity, with a Int64 as the ID and a few other fields linked to the username entity via a 1 username can have 0 to many messages relationship.

I have two repositories (basic all, add, delete, save methods) for username and message entities.

But I want to associate a message entity that I generate in code (new Message() etc) with a username how would I do this with a repository and across two different objectcontexts?

I got it working via not using a repository via:

usernameEntity.Messages.Add(msg);

Anyone got any hints, rather confused as if I could split this type of logic into two different repositories.

  • 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-20T20:46:31+00:00Added an answer on May 20, 2026 at 8:46 pm

    You will simply call:

    var user = repository.GetUserById(id);
    user.Messages.Add(new Message(...));
    Save(); // I don't know how do you save changes.
    

    Your repositories should share a single context for one unit of work (a business transaction). You should have some method (Save, Commit or whatever else) which will save changes made on entities loaded from all repositories. This is usually handled by a separate class implementing the unit of work pattern which wrapps context and is shared among repositories. Saving changes is called on unit of work instead of repository. There is plenty of related questions to this problem.

    In case of real foreign key relationship modeled in EFv4 you can also use simply:

    var message = new Message(...);
    message.UserId = id; // Foreign key property exposed on Message entity
    repository.Insert(message);
    Save();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have a class called Customer, which contains the following fields: UserName
Lets say I have a text file with the following data Username User ID
Lets say I have the following one-to-many relationship: Site has many Users User belongs
Lets say you have an program like this: $info = array( 'username'=>'jeff', 'password'=>'kay' );
Lets say have this immutable record type: public class Record { public Record(int x,
Lets say I have a checkbox with id 'test' and its on a page
Lets say I have an abstract base class and a derived class with some
lets say i have a class defined as shown below. Lets now say i
Lets say I have three divs in my HTML <div> <h2>This is div 1</h2>
Lets say I have a field. Lets call it Barcode1. Right now all Barcodes1

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.