c# - System.TypeInitializationException:''Bid' 的类型初始化程序引发了异常。'
问题描述
我有一个服务层,目标框架设置为.NET Standard 2.0
. 一些服务使用DbContext
来自EntityFrameWorkCore
.
有两个 Web 项目使用此服务层:
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
项目中我收到此错误:
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
类不是我的数据模型,我也不了解它
解决方案
我们遇到了这个问题,并通过将 EF 核心更改为 EF 6.4.4 来解决它可能是您有这样的问题,需要更改或降级您的 EF 版本(如果您使用 EF)
推荐阅读
- google-cloud-platform - 从 Bigquery 中的原始 Google 分析会话数据替换流量源?
- javascript - 如何检测云火库功能 onUpdate() 中添加的新字段
- python - 如何比较两个str值数据框python pandas
- python-3.x - 为什么以小数倒数会导致 xxx.9999999999999999999x?
- java - Transformer Kafka 中的 ManagedChannel 线程安全吗
- performance - RandomUtils.nextLong(10L,1000L) 失败,JMeter HTTP 请求正文中出现“意外令牌:10L”
- mouse - Is there a way to duplicate my mouse input position to another PC through local network?
- configuration - How do I tell figwheel to start and connect to Google Chrome instead of Firefox?
- python-3.x - 如何用字符串联系表情符号
- django - Nginx 路由 Django Web App 链接到错误的路径