2017-09-26 203 views
0

我终于开始在我们的旧API中实现Linq了,所以我开始制作与我们的表和数据库的DB类相关的所有类,我遵循在线guid,但是我可以'让它工作。我有我的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) { } 
    } 
} 

而对于我NewsItems.cs类文件:

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] 
     ... 
    } 
} 

所有类和字段都是公开的,不过它仍然引发以下错误:

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

+0

何处或什么是'表'类型? – rene

+1

看起来你的'Table'类型不是'public',就像你的类'NewsItems'一样。正如@rene所说,你的类型“Table”是什么,它是如何定义的?使用什么访问修饰符? – benichka

+0

@rene这是linq包含的类,这就是让我困惑的。 'class System.Data.Linq.Table where TEntity:class' –

回答

2

可访问性不一致错误意味着Table类(即Table<T>)可以声明&初始化为private,将其设置为public,使其具有与相同的news_items

因此,如果你有Table类像这样的地方:

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

您需要将其设置为public水平所要求的news_items场:

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

参考:

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

+1

@Ippickle我也这么认为,但如果是这种情况,错误应该显示'TableAttribute'。 –

+0

'TableAttribute'不同于'Table '在你的情况下......'TableAttribute'不是通用的,必须与'[Table(“TableName”)]'一起使用'而不是声明为类型。 –

+0

如果我这样做,我得到这个错误:错误 \t CS1729 \t 'TableAttribute' 不包含一个构造函数1个参数\t NewsAPI \t K:\集团\ repo_groclaes_rawapi \ NewsAPI \型号\ NewsItems.cs 活动 –

相关问题