3

I've finally started implementing Linq in our old API so I started making all the classes associated with our Tables and DB classes for the Database I followed an online guid but I can't get it working. I have the following code for my Company.cs:

using RAW_API.Models;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace RAW_API.DataContexts {
    [Database]
    public class Company : DataContext {
        public Table<NewsItems> news_items;
        public Company( string connection ) : base( connection ) { }
    }
}

And for my NewsItems.cs class file:

using System;
using System.Data.Linq.Mapping;

namespace RAW_API.Models {
    [Table( Name = "news_items" )]
    public class NewsItems {
        [Column( IsPrimaryKey = true, IsDbGenerated = true )]
        public int id { get; set; }
        [Column]
        public string titleNL { get; set; }
        [Column]
        ...
    }
}

all classes and fields are public but it still throws the following error:

Error CS0052: Inconsistent accessibility: field type 'Table' is less accessible than field 'Company.news_items' ApplicationServerWrapper K:\Group\repo_groclaes_rawapi\ApplicationServerWrapper\DataContexts\Company.cs:8

  • Where or what is the `Table` type? – rene Sep 26 '17 at 07:06
  • 1
    It seems like your `Table` type is not `public`, as your class `NewsItems`. As @rene said, what is your type `Table` and how is it defined? With what access modifier? – benichka Sep 26 '17 at 07:17
  • @rene It's the included class from linq, that's what confuses me. 'class System.Data.Linq.Table where TEntity : class' – Jamie Vangeysel Sep 26 '17 at 07:23

2 Answers2

8

Inconsistent accessibility error means that Table class (i.e. Table<T>) may declared & initialized as private, set it to public so that it has same accessibility level as news_items.

Hence, if you have Table class somewhere like this:

// T is the table class name
class Table<T>
{
    // other stuff
}

You need to set it as public level as required by news_items field:

public class Table<T>
{
    // other stuff
} 

Reference:

Inconsistent accessibility: field type 'world' is less accessible than field 'frmSplashScreen

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • 1
    @Icepickle I thought so too, but the error should show `TableAttribute` instead if that was the case. – Manfred Radlwimmer Sep 26 '17 at 07:19
  • `TableAttribute` is different from `Table` in your case... `TableAttribute` is not generic & must be used with `[Table("TableName")]` instead of declared as type. – Tetsuya Yamamoto Sep 26 '17 at 07:22
  • If I do that I get this error: Error CS1729 'TableAttribute' does not contain a constructor that takes 1 arguments NewsAPI K:\Group\repo_groclaes_rawapi\NewsAPI\Models\NewsItems.cs 5 Active – Jamie Vangeysel Sep 26 '17 at 07:27
2

Here from MS DOCs

Compiler Error CS0052
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0052

// CS0052.cs
public class MyClass2
{
   // The following line causes an error because the field, M, is declared
   // as public, but the type, MyClass, is private. Therefore the type is
   // less accessible than the field.
public MyClass M;   // CS0052

private class MyClass
{
}
   // One way to resolve the error is to change the accessibility of the type
   // to public. 
   //public class MyClass
   // Another solution is to change the accessibility of the field to private.
   //  private MyClass M; 
}

public class MainClass
{
    public static void Main()
    {
    }
}
Valentin Petkov
  • 1,570
  • 18
  • 23