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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:28:41+00:00 2026-05-27T16:28:41+00:00

We have 2 tables: Table Authority: public class Authority { public int ID {get;set;}

  • 0

We have 2 tables:

Table Authority:

public class Authority
{
   public int ID {get;set;}
   public string Name{get;set;}
      ...
}

Table Agents

public class Agent
{
  public int ID{get;set;}
  public int FirstName{get;set;}
}

And we have a relationship many-to-many between these two tables:

public class AuthorityConfiguration : EntityTypeConfiguration<Authority>
{
    public AuthorityConfiguration()
        : base()
    {
        HasKey(p => p.ID);

        HasMany(p => p.Agents).WithMany(a => a.Authorities).Map(mc =>
            {
                mc.MapLeftKey("AuthorityID");
                mc.MapRightKey("AgentID");
                mc.ToTable("AuthorityAgent");
            });


        ToTable("Authority");

    }
}

Everything is working fine.
But now I have a page to create an association between the tables and I need to insert into my table “authorityAgent” the relationship using Repository Pattern.

Problem 1: How can I get the Agent if my DAO is receiving an Authority?

AuthorityDAO.cs

public static void InsertAgent(int authorityID, int agentID)
    {
        var dao = new ConcreteDAO<Authority>();

        Authority authority = dao.Single(p => p.ID.Equals(authorityID));

        // I can't do that because the relationship doesn't exist yet.
        var agent = authority.Agents.Where(p => p.ID.Equals(agentID));


        authority.Agents.Add(agent);

        dao.Attach(authority);

        dao.SaveChanges();

    }

I know I can create my context in the DAO to do it, but I will brake the Pattern, won’t I?

How can I do the method above?

Thank you.

EDIT: I found a solution but I don’t know if it’s the better way to do it:

I created a constructor to my ConcreteDAO passing the ObjectContext and a method to get my object context:

GenericDAO.cs

public ObjectContext GetContext()
{
     return _context;
}

ConcreteDAO.cs

public ConcreteDAO()
{

}

public ConcreteDAO(ObjectContext context)
    : base(context)
{
}

And Inside my AuthorityDAO.cs

 public static void InsertAgent(int authorityID, int agentID)
 {
     var dao = new ConcreteDAO<Authority>();
     Authority authority = dao.Single(p => p.ID.Equals(authorityID));
     dao.Attach(authority);

     var daoAgent = new ConcreteDAO<Agent>(dao.GetContext());

     var agent = daoAgent.Single(p => p.ID == agentID);

     authority.Agents.Add(agent);

     dao.SaveChanges();
}
  • 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-27T16:28:42+00:00Added an answer on May 27, 2026 at 4:28 pm

    I think you are having this problem because you are using the Repository pattern without the Unit Of Work pattern. Your ConcreteDAO<T> (= generic repository for entity type T, I guess) should not create a context (=unit of work). Instead your consuming method should create it explicitly and inject it into all repositories you need. You last method would then look like this:

    public static void InsertAgent(int authorityID, int agentID)
    {
        using (var unitOfWork = new UnitOfWork()) // unit of work = context
        {
            var daoAuthority = new ConcreteDAO<Authority>(unitOfWork);
            var daoAgent = new ConcreteDAO<Agent>(unitOfWork);
    
            var authority = daoAuthority.Single(p => p.ID.Equals(authorityID));
            var agent = daoAgent.Single(p => p.ID == agentID);
    
            authority.Agents.Add(agent);
    
            unitOfWork.SaveChanges();
        }
    }
    

    In many situations where changing relationships are involved you need more than one generic repository, but all work has to be done within the same context.

    You can, btw, save to load the entities from the database because you know the primary key properties and don’t want to change the entities themselves but only a relationship. In that case you can work with attached “stub” entities:

    public static void InsertAgent(int authorityID, int agentID)
    {
        using (var unitOfWork = new UnitOfWork())
        {
            var daoAuthority = new ConcreteDAO<Authority>(unitOfWork);
            var daoAgent = new ConcreteDAO<Agent>(unitOfWork);
    
            var authority = new Authority { ID = authorityID,
                Agents = new List<Agent>() };
            daoAuthority.Attach(authority);
    
            var agent = new Agent { ID = agentID };
            daoAgent.Attach(agent);
    
            authority.Agents.Add(agent);
    
            unitOfWork.SaveChanges();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have two tables: Authority , Permission they have many to many relationship class
I have two tables: Table 1: ID, PersonCode, Name, Table 2: ID, Table1ID, Location,
Suppose you have these tables: Table Name: Salesman Fields: S_ID(Primary Key), Name Table Name:
Here's the scenario: I have 2 tables: CREATE TABLE dbo.API_User ( id int NOT
I have a table of 3 columns: AuthorID (id can be repeated) JournalName (name
i have 2 tables CREATE TABLE if not exists tag_name( + tagid INTEGER PRIMARY
I have 2 tables: CREATE TABLE `product_det` ( `id` bigint(12) unsigned NOT NULL AUTO_INCREMENT,
I have several tables: CREATE TABLE [dbo].[Tracks]( [Id] [uniqueidentifier] NOT NULL, [Artist_Id] [uniqueidentifier] NOT
Say I have three tables: Fruit (Table 1) ------ Apple Orange Pear Banana Produce
I have two tables: a source table and a target table. The target table

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.