Tag: .net core

entity framework核心:私有或受保护的导航属性

是否有可能在EFCore中定义具有私有或受保护访问级别的导航属性,以使这种代码工作: class Model { public int Id { get; set; } virtual protected ICollection childs { get; set; } }

如何获得AssemblyTitle?

我知道Assembly.GetExecutingAssembly()的.NET Core替换是typeof(MyType).GetTypeInfo().Assembly ,但是替换为 Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false) 我已经尝试在组装之后附加代码的最后一位用于提到的第一个解决方案,如下所示: typeof(VersionInfo).GetTypeInfo().Assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute)); 但它给了我一个“不能隐式转换为object []的消息。 更新:是的,正如下面的评论所示,我认为它与输出类型相关联。 这是代码片段,我只是想将其更改为与.Net Core兼容: public class VersionInfo { public static string AssemlyTitle { get { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false); // More code follows 我已经尝试将其更改为使用CustomAttributeExtensions.GetCustomAttributes(但我目前不了解c#以了解如何实现与上面相同的代码。我仍然对MemberInfo和Type等有所了解。非常感谢任何帮助!

EF Core – 表’* .__ EFMigrationsHistory’不存在

我想强调这是.NET Core,而关于EF 6.0的线程不适用于这个问题 我创建了我的DbContext并将其添加到DI中,但是当我执行dotnet ef database update -v它不想创建迁移表__EFMigrationsHistory 。 是否还有其他一些我应该先做的命令,或者这是EF Core MySQL适配器的错误? MainDbContext using Microsoft.EntityFrameworkCore; using MySQL.Data.EntityFrameworkCore.Extensions; using Web.Models; namespace Web.Infrastructure { public class MainDbContext : DbContext { public DbSet Users { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseMySQL(“connection-string-here”); base.OnConfiguring(optionsBuilder); } } } 错误 查找DbContext类… 使用上下文’MainDbContext’。 在服务器’localhost’上使用数据库’db’。 MySql.Data.MySqlClient.MySqlException:表’db .__ EFMigrationsHistory’不存在 表’db .__ […]

将NLog连接并注入.Net Core控制台应用程序

我创建了一个消费者/工作,我将作为一个用C#编写的Linux进程运行。 该过程将: 阅读RabbitMQ的消息 对数据库进行更改 记录任何错误 关于.net核心的nlog上的所有文档都在aspnet核心上。 当我尝试获得ILogger实现时,它返回null。 这是除了接线和使用之外: static void ConfigureServices() { string environment = Environment.GetEnvironmentVariable(“ASPNETCORE_ENVIRONMENT”); var builder = new ConfigurationBuilder() .SetBasePath(Path.Combine(AppContext.BaseDirectory)) .AddJsonFile(“appsettings.json”, optional: true, reloadOnChange: true) .AddJsonFile($”appsettings.{environment}.json”, optional: true); var services = new ServiceCollection(); Configuration = builder.Build(); […] services.AddLogging(); ServiceProvider = services.BuildServiceProvider(); var loggerFactory = ServiceProvider.GetService(); loggerFactory.AddNLog(); } static void Main(string[] args) { ConfigureServices(); […]

.net Core X Forwarded Proto无法正常工作

我正在努力让我的.net核心1.1应用程序在负载均衡器后面工作并强制执行https。 我的Startup.cs中有以下设置 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IServiceProvider serviceProvider, IOptions auth0Settings) { loggerFactory.AddConsole(Configuration.GetSection(“Logging”)); loggerFactory.AddDebug(); var startupLogger = loggerFactory.CreateLogger(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); app.UseBrowserLink(); startupLogger.LogInformation(“In Development”); } else { startupLogger.LogInformation(“NOT in development”); app.UseExceptionHandler(“/Home/Error”); } app.UseMiddleware(); app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto });` app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationScheme= CookieAuthenticationDefaults.AuthenticationScheme, AutomaticAuthenticate = true, […]

.net核心dependency injection是否支持Lazy

我试图使用通用的Lazy类来实例化.net核心dependency injection扩展的昂贵类。 我已经注册了IRepo类型,但我不确定Lazy类的注册是什么样的,或者它是否支持。 作为一种解决方法,我使用了这种方法http://mark-dot-net.blogspot.com/2009/08/lazy-loading-of-dependencies-in-unity.html 配置: public void ConfigureService(IServiceCollection services) { services.AddTransient(); //register lazy } 控制器: public class ValuesController : Controller { private Lazy _repo; public ValuesController (Lazy repo) { _repo = repo; } [HttpGet()] public IActionResult Get() { //Do something cheap if(something) return Ok(something); else return Ok(repo.Value.Get()); } }

如何从UWP(又名.NET Core)中的Type对象获取程序集

Type类在.NET Framework中具有Assembly属性。 但是,当您编写当然使用.NET Core的UWP时,此属性已消失。 只有AssemblyQualifiedName属性可用。 我怎样才能从这个名字进入大会? 请记住,.NET Core中没有很多常用类,因此您通常的.NET Framework答案可能无效。 例如,没有AppDomain等等。

运行`serviceProvider.GetService ();`时,.Net core 2.0控制台程序得到空值。

以下.Net core 2.0控制台应用程序在运行serviceProvider.GetService();时得到null值serviceProvider.GetService(); ? class Program { static void Main(string[] args) { var services = new ServiceCollection(); ConfigureServices(services); var serviceProvider = services.BuildServiceProvider(); var app = serviceProvider.GetService(); // Got null value Task.Run(() => app.Run()).Wait(); } private static void ConfigureServices(IServiceCollection services) { ILoggerFactory loggerFactory = new LoggerFactory() .AddConsole() .AddDebug(); services.AddSingleton(loggerFactory); // Add first my already configured instance […]

无法为netstandard1.x项目安装Netstandard1.x Nuget软件包

在完成全新的Windows 10安装以及最新的Visual Studio 2015,netcore和nuget工具之后 – 我无法再将Nuget软件包安装到任何.net标准项目中。 这是一些示例输出: Restoring packages for ‘ClassLibrary1’. Restoring packages for c:\users\zone1\documents\visual studio 2015\Projects\ClassLibrary1\ClassLibrary1\project.json… Package System.ComponentModel.EventBasedAsync 4.0.11 is not compatible with netstandard1.3 (.NETStandard,Version=v1.3). Package System.ComponentModel.EventBasedAsync 4.0.11 supports: – monoandroid10 (MonoAndroid,Version=v1.0) – monotouch10 (MonoTouch,Version=v1.0) – net45 (.NETFramework,Version=v4.5) – netcore50 (.NETCore,Version=v5.0) – netstandard1.0 (.NETStandard,Version=v1.0) – netstandard1.3 (.NETStandard,Version=v1.3) – portable-net45+win8+wp8+wpa81 (.NETPortable,Version=v0.0,Profile=Profile259) – win8 (Windows,Version=v8.0) – […]

.Net Standard中CompileToMethod的替代品

我现在正在移植一些使用表达式到.Net Core应用程序的库,并遇到了一个问题,我的所有逻辑都基于LambdaExpression.CompileToMethod ,它只是遗漏了。这是示例代码: public static MethodInfo CompileToInstanceMethod(this LambdaExpression expression, TypeBuilder tb, string methodName, MethodAttributes attributes) { … var method = tb.DefineMethod($”__StaticProxy”, MethodAttributes.Private | MethodAttributes.Static, proxy.ReturnType, paramTypes); expression.CompileToMethod(method); … } 是否有可能以某种方式重写它以使用表达式生成方法成为可能? 我已经可以用Emit做到这一点,但它非常复杂,我想避免它支持高级表达式。 我尝试使用var method = expression.Compile().GetMethodInfo(); 但在这种情况下,我收到一个错误: System.InvalidOperationException:无法从其他模块导入全局方法或字段。 我知道我可以手动发出IL,但我需要将Expression – >转换为绑定到特定TypeBuilder MethodInfo ,而不是在其上构建自己的DynamicMethod 。