有人可以幫我弄清楚我哪里出錯了。我正在嘗試為 .Net Core 中的服務實現通用擴展方法。這是我的界面 -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 { }然后我有我的通用擴展 -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; }}我的服務 -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; }}現在編譯器抱怨說 -“ContactService”未實現接口成員“IAddable.Context”當我嘗試打電話時,service.Add(contact)我得到 -IContactService 不包含 Add 的定義,并且找不到接受 IContactService 類型的第一個參數的可訪問擴展方法。我已經在另一個項目中使用了它,但是對于我的一生,我無法弄清楚為什么它在這里不起作用......
1 回答

翻閱古今
TA貢獻1780條經驗 獲得超5個贊
您聲明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) 應該是一個屬性:
public class ContactService : IContactService
{
public AppContext Context { get; }
//...
- 1 回答
- 0 關注
- 118 瀏覽
添加回答
舉報
0/150
提交
取消