有人可以幫我弄清楚我哪里出錯(cuò)了。我正在嘗試為 .Net Core 中的服務(wù)實(shí)現(xiàn)通用擴(kuò)展方法。這是我的界面 -public interface IContactService : IAddable<Contact>{ Task<List<Contact>> GetAll();}我的模型 -public partial class Contact : IBaseEntity{ public int Id { get; set; } public string Name { get; set; } public string Email { get; set; } public string Phone { get; set; }}以及模型的界面 -public interface IBaseEntity { }然后我有我的通用擴(kuò)展 -public interface IAddable<T>{ AppContext Context { get; }}public static class IAddableExentions{ public static async Task<T> Add<T>(this IAddable<T> addable, T entity) where T : class, IBaseEntity { await addable.Context.Set<T>().AddAsync(entity); await addable.Context.SaveChangesAsync(); return entity; }}我的服務(wù) -public class ContactService : IContactService{ public AppContext Context; public ContactService(AppContext context) { Context = context; } public async Task<List<Contact>> GetAll() { var contacts = await Context .Contacts .ToListAsync(); return contacts; }}現(xiàn)在編譯器抱怨說(shuō) -“ContactService”未實(shí)現(xiàn)接口成員“IAddable.Context”當(dāng)我嘗試打電話時(shí),service.Add(contact)我得到 -IContactService 不包含 Add 的定義,并且找不到接受 IContactService 類(lèi)型的第一個(gè)參數(shù)的可訪問(wèn)擴(kuò)展方法。我已經(jīng)在另一個(gè)項(xiàng)目中使用了它,但是對(duì)于我的一生,我無(wú)法弄清楚為什么它在這里不起作用......
1 回答

翻閱古今
TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超5個(gè)贊
您聲明Context為ContactService,
public class ContactService : IContactService
{
public AppContext Context; //<-- field
//...
但是IAddable<T>界面
public interface IAddable<T>
{
AppContext Context { get; }
}
of which IContactServiceis derived from, 指示它 ( Context) 應(yīng)該是一個(gè)屬性:
public class ContactService : IContactService
{
public AppContext Context { get; }
//...
- 1 回答
- 0 關(guān)注
- 115 瀏覽
添加回答
舉報(bào)
0/150
提交
取消