2017-09-26 113 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 活動 –

相關問題