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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:15:13+00:00 2026-06-05T04:15:13+00:00

We are attempting to use a repository pattern for an EF project. The user

  • 0

We are attempting to use a repository pattern for an EF project. The user is allowed to select one of three separate (but structurally identical) databases to log on to (there is one for training, one for testing, and one for production).

We are currently using an EF 4 implementation and have generated our T4 via the ADO.Net EntityObject Generator. Our repository base class looks as follows:

public class RepositoryBase<C> : IDisposable
    where C : ObjectContext, new()
{
    private C _DataContext;

    public virtual C DataContext
    {
        get
        {
            if (_DataContext == null)
            {
                _DataContext = new C();
            }
            return _DataContext;
        }
    }

    //other code cut for brevity...
}

I think what we want is the ability to modify the “_DataConext = new C();” line so that it can use a connection string that is generated at runtime to point to the proper database. Unfortunately passing the connection string like this: _DataConext = new C(connectionString); is not allowed and results in this message: “Arguments cannot be passed to a ‘New’ used on a type parameter.”

I can see in the model.tt file’s code behind that there are three contructors: the default parameterless constructor, a constructor that has an EntityConnection parameter, and a third which has a connection string parameter (the one we want to use).

The question is how do we go about doing this? Any help is appreciated!

  • 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-06-05T04:15:14+00:00Added an answer on June 5, 2026 at 4:15 am

    Well, problem is specifying a constructor with parameter on a generic type.. This is not allowed.
    First solution would be to use reflection.
    If you know what are the constructors defined:

    public virtual C DataContext
    {
        get
        {
            if (_DataContext == null)
            {
                _DataContext = (C)Activator.CreateInstance(typeof(C), new object[]{ connectionString });
            }
            return _DataContext;
        }
    }
    

    A better solution would be to give the instance of C to the construtor of you repository.

    Hope it helps

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

Sidebar

Related Questions

I'm attempting to use the Repository Pattern to write a data access layer on
In attempting to use std::select1st from <functional> in a VS2008 project I found that
Im attempting to use websockets for a project. It needs to use the ipad,
I'm attempting to implement a repository pattern with my poco objects auto generated from
I'm attempting to use Ninject to inject repositories into controllers of my MVC project.
Question: I'm new to the repository pattern, and I'm attempting to implement a new
I am attempting use Java Pathfinder and I have pathfinder working. import gov.nasa.jpf.jvm.Verify; user.java:2:
I am attempting to use OpenSSL's SHA512 function in a C++ project of mine.
I've got the following line in my pom.xml: <updateRepositoryURL>file:/${user.home}/.m2/repository/repository.xml</updateRepositoryURL> and when I am attempting
I am attempting to write some Tests for a small project of mine but

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.