- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试做一个网络项目,但遇到了问题。
代码:
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSingleton(Configuration);
services.AddScoped<ILibraryAssetService, LibraryAssetService>();
services.AddDbContext<LibraryContext>(options => options.UseSqlServer(Configuration.GetConnectionString("LibraryConnection")));
}
但是我收到错误:
Error CS0311 The type 'Library.LibraryAssetService' cannot be used as type parameter 'TImplementation' in the generic type or method 'ServiceCollectionServiceExtensions.AddScoped(IServiceCollection)'. There is no implicit reference conversion from 'Library.LibraryAssetService' to 'LibraryData.ILibraryAssetService'. Library C:\Users\Austina\source\repos\Library\Library\Startup.cs 29 Active
我用谷歌到处搜索,但没有任何帮助。也许我遗漏了一些明显的东西?
还可以使用类进行编码:
public class LibraryAssetService : ILibraryAssetService
{
private LibraryContext _context;
private int videoId;
public LibraryAssetService(LibraryContext context)
{
_context = context;
}
public LibraryAssetService()
{
}
public void Add(LibraryAsset newAsset)
{
_context.Add(newAsset);
_context.SaveChanges();
}
public LibraryAsset Get(int id)
{
return _context.LibraryAssets
.Include(a => a.Status)
.Include(a => a.Location)
.FirstOrDefault(a => a.Id == id);
}
public IEnumerable<LibraryAsset> All => _context.LibraryAssets
.Include(a => a.Status)
.Include(a => a.Location);
public int Id => throw new NotImplementedException();
public string ImageUrl => throw new NotImplementedException();
public string Title => throw new NotImplementedException();
public string GetAuthorOrDirector(int id)
{
var isBook = _context.LibraryAssets
.OfType<Book>().Any(a => a.Id == id);
var isVideo = _context.LibraryAssets
.OfType<Video>().Any(video => videoId == id);
return isBook
? GetAuthor(id)
: GetDirector(id);
}
public LibraryAsset GetById(int id)
{
return _context.LibraryAssets
.Include(asset => asset.Status)
.Include(asset => asset.Location)
.FirstOrDefault(asset => asset.Id == id);
}
public LibraryBranch GetCurrentLocation(int id)
{
return _context.LibraryAssets.FirstOrDefault(a => a.Id == id).Location;
}
public string GetDeweyIndex(int id)
{
if (_context.Books.Any(book => book.Id == id))
{
return _context.Books.FirstOrDefault(book => book.Id == id).DeweyIndex;
}
else
{
return "";
}
}
public string GetIsbn(int id)
{
if (_context.Books.Any(a => a.Id == id))
{
return _context.Books
.FirstOrDefault(a => a.Id == id).ISBN;
}
else
{
return "";
}
}
public LibraryCard GetLibraryCardByAssetId(int id)
{
return _context.LibraryCards
.FirstOrDefault(c => c.Checkouts
.Select(a => a.LibraryAsset)
.Select(v => v.Id).Contains(id));
}
public string GetTitle(int id)
{
return _context.LibraryAssets.First(a => a.Id == id).Title;
}
public string GetType(int id)
{
var book = _context.LibraryAssets.OfType<Book>()
.Where(b => b.Id == id);
return book.Any() ? "Book" : "Video";
}
private string GetAuthor(int id)
{
var book = (Book)Get(id);
return book.Author;
}
private string GetDirector(int id)
{
var video = (Video)Get(id);
return video.Director;
}
public IEnumerable<ILibraryAssetService> GetAll()
{
throw new NotImplementedException();
}
internal class LibraryContext_context
{
}
}
最佳答案
这应该是由命名空间冲突引起的,您有两个具有相同名称的类或接口(interface),它们位于不同的命名空间中。
尝试使用fully qualified namespaces喜欢
services.AddScoped<LibraryData.ILibraryAssetService, Library.LibraryAssetService>();
您需要更正上述完全限定的命名空间,因为 Library.LibraryAssetService
未实现 LibraryData.ILibraryAssetService
,如编译错误所示。
关于c# - 没有从 'type1' 到 'type2' 的隐式引用转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50792804/
我是一名优秀的程序员,十分优秀!