У меня есть сервисный уровень с целевой структурой, установленной на .NET Standard 2.0
. Некоторые сервисы используют DbContext
из EntityFrameWorkCore
.
. Существует два веб-проекта, которые используют этот слой сервиса:
ASP.NET Core 3.0 (Blazor)
ASP.NET MVC 4.6.2
В проекте Core
используется внедрение зависимостей следующим образом (сокращено):
public void ConfigureServices(IServiceCollection services)
{
var json = File.ReadAllText("appsettings.json");
var settings = JsonConvert.DeserializeObject<Settings>(json);
services.AddScoped(provider => new CompetitorContext(settings.ConnectionStrings.Web));
services.AddScoped<ICompetitorService, CompetitorService>();
}
В проекте MVC
используется внедрение зависимостей через Unity следующим образом (сокращено):
public static void RegisterComponents()
{
_container = new UnityContainer();
var settings =
JsonConvert.DeserializeObject<Settings>(
File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
@"appsettings.json")));
_container.RegisterInstance(typeof(CompetitorContext), new CompetitorContext(settings.ConnectionStrings.Web));
_container.RegisterType<ICompetitorService, CompetitorService>();
DependencyResolver.SetResolver(new UnityDependencyResolver(_container));
}
При вызове метода одного из сервисов, который выполняет вызов CompetitorContext
(который наследуется от DbContext
), он отлично работает в проекте Core
, но в проекте MVC
I получите эту ошибку:
System.TypeInitializationException
HResult=0x80131534
Message=The type initializer for 'Bid' threw an exception.
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
This exception was originally thrown at this call stack:
Bid.internalInitialize()
Bid.Bid()
Inner Exception 1:
EntryPointNotFoundException: Unable to find an entry point named 'DllBidEntryPoint' in DLL 'System.Data.dll'.
Другие службы, которые не используют EntityFrameWorkCore
, работают нормально, поэтому я думаю, что проблема заключается в использовании EntityFrameWorkCore
вместе с проектом .NET MVC 4.6.2
, но я не уверен.
В чем может быть проблема с этим исключением или как я могу узнать больше информации?
Отредактировано: CompetitorService
:
public class CompetitorService : ICompetitorService
{
private readonly CompetitorContext _ctx;
public CompetitorService(CompetitorContext ctx)
{
_ctx = ctx;
}
public IList<CompetitorModel> GetAll()
{
return _ctx.Competitors.OrderBy(c => c.ID).ToList();
}
public CompetitorModel GetById(int id)
{
return this.GetAll().Where(c => c.ID == id).First();
}
public CompetitorModel GetByCompetitorKey(string competitorKey)
{
return this.GetAll().Where(c => c.CompetitorKey == competitorKey).FirstOrDefault();
}
}
CompetitorContext
:
public class CompetitorContext : DbContext
{
public DbSet<CompetitorModel> Competitors { get; set; }
public CompetitorContext(string connectionString) :
base(SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), connectionString).Options)
{
}
}
CompetitorModel
:
[Table("vw_competitors")]
public class CompetitorModel
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ID { get; set; }
public string CompetitorKey { get; set; }
public string Name { get; set; }
public string ShortName { get; set; }
public string Experience { get; set; }
public string BBB_Rating { get; set; }
public string BBB_URL { get; set; }
public string BBB_Complaints { get; set; }
public string BBB_Accredited { get; set; }
public string TrustPilot_URL { get; set; }
public string TrustPilot_Rating { get; set; }
public string TrustPilot_Reviews { get; set; }
public string RipoffReport_Complaints { get; set; }
public string MoneyBackGuarantee { get; set; }
public DateTime LastUpdated { get; set; }
}
Примечание: Указанный класс Bid
не является моделью данных мой, и я не знаю об этом