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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:57:07+00:00 2026-06-06T01:57:07+00:00

While doing the following simple example, I found the following difficulties As the title

  • 0

While doing the following simple example, I found the following difficulties
As the title says, I am intending to use the Repository pattern while I am storing data in the Azure table storage.now I have couple of classes, Repository.cs, IRepository.cs, DataContext.cs and the Controller.

During my reading I found some info and been doing as follows.
IRepository.cs

public interface IRepository<T> where T: TableServiceEntity
{

    T GetById(int Id);

    IQueryable<T> GetAll();

}

and the DataContext.cs

public class DataContext<T>:TableServiceContext where T:TableServiceEntity
{


   public DataContext(CloudStorageAccount storageaccount, StorageCredentials credentials)
        : base(storageaccount.TableEndpoint.AbsoluteUri, credentials) 
    {
       // _storageAccount = storageaccount;

       var  storageAccount =        CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(KEY_STORAGE));
        storageAccount.CreateCloudTableClient().CreateTableIfNotExist(tableName);


    }

   public IQueryable<T> DeviceTable
   {
       get { return CreateQuery<T>(tableName); }
   }


}

plus some part of the controller(I have already data in the table which I created before)

public class DeviceMeController : Controller
{

    private IRepository<Entity>_repository;

    public Controller() : this(new Repository<Entity>()) 
    {
    }
    public Controller(IRepository<Entity> repository) 
    {
        _repository = repository;
    }
    public ActionResult Index()
    {
        var List = _repository.GetAll();

        return View(deviceList);
    }

and the the Implementation of the interface Reposistory.cs, here is where I have an error and got lost somewhere

 public class Repository<T>:IRepository<T> where T:TableServiceEntity
   {
  private  DataContext<T> _serviceContext;
    // here get tablename as pararameter;
    // so the Enities call this function 
    public Repository()
    {

        // the same context for multiple tables ? 
    }
    // perhaps it should take the table Name
    public void Add(T item)
    {

        _serviceContext.AddObject(TableName,item);
        _serviceContext.SaveChangesWithRetries();
    }
  public IQueryable<T> GetAll()
    {
       var results = from c in _serviceContext.Table
                      select c;

        return results;

Error is about the null reference, the debugger shows the variable results is null?

In the end I need to know few things.

what should I do in the Repository.cs constructor? I believe the Datacontext.cs class has to be in a separate class …

any Hint here

  • 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-06T01:57:08+00:00Added an answer on June 6, 2026 at 1:57 am

    Hy,

    first of all I presume you left out some code, because I don’t see how you get your context in your repository. But supposing you do set it correctly, (injection?) taking into account the way you desinged your datacontext the repository doesn’t need to know the table name because it is set in the following lines of code:

     public IQueryable<T> DeviceTable 
     { 
         get { return CreateQuery<T>(Constants.DeviceTableName); } 
     } 
    

    So when you create a query based on the IQueryable DeviceTable, the table name is already set.

    The thing is I don’t see the need for your context class, especially as it can only bring over a single entity type (it is generic and based on an entity).
    A basic layout of my Repository for Azure Table Storage is:

    public abstract class CloudRepository<TEntity> : ICloudRepository<TEntity>
    {
        private TableServiceContext _tableServiceContext;
        private string _tableName;
    
        public string TableName
        {
            get { return _tableName ?? ( _tableName = typeof(TEntity).Name.Replace("Entity", string.Empty).ToLower()); }
        }
    
        public CloudStorageAccount StorageAccount
        {
            get
            {
                return CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));
            }
        }
    
        public CloudTableClient TableClient
        {
            get
            {
                CloudTableClient cloudTableClient = StorageAccount.CreateCloudTableClient();
                cloudTableClient.CreateTableIfNotExist(TableName);
                return cloudTableClient;
            }
        }
    
        public TableServiceContext ServiceContext
        {
            get
            {
                return _tableServiceContext ?? (_tableServiceContext = TableClient.GetDataServiceContext());
            }
        }
    
        public IEnumerable<TEntity> FindAll()
        {
            return ServiceContext.CreateQuery<TEntity>(TableName).ToList();
        }
    
    }
    

    Hope this helps you.

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

Sidebar

Related Questions

While doing some JavaScript performance tests I came up with the following piece of
While doing some refactoring I've found that I'm quite often using a pair or
While doing performance testing on windows, i usually use perfmon. But when i put
I get this problem while doing some experiment with set . I use a
I have the following example data frame x id val a 1 a 2
Consider the following simple example: f[x_] = Sin[5 x] + Sin[3 x]; p1 =
While doing some random experimentation with a factorial program in C, Python and Scheme.
While doing some small regex task I came upon this problem. I have a
while doing some homework in my very strange C++ book, which I've been told
while doing LINQ I got this Error. Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'.

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.