草庐IT

c# - 如何使用 EF 6 Fluent Api 添加复合唯一键?

我有一个表(Id,name,itemst,otherproperties),Id是主键,我想要一个唯一的组合键(name,itemst)。我如何通过流畅的API(首选)或注释首先使用代码添加它? 最佳答案 假设您有一个名为的实体publicclassMyTable{publicintId{get;set;}publicStringName{get;set;}}您可以使用以下方法创建组合键publicclassYourContext:DbContext{publicDbSetMyTables{get;set;}protectedove

c# - 如何在 .NET Core MVC 项目中转换 appsettings.json?

我已经在我的项目中添加了额外的json配置文件appsettings.DEV.jsonappsettings.QA.json并根据环境将它们加载到Startup函数中:publicStartup(IHostingEnvironmentenv){varbuilder=newConfigurationBuilder().SetBasePath(env.ContentRootPath).AddJsonFile("appsettings.json",optional:false,reloadOnChange:true).AddJsonFile($"appsettings.{env.Enviro

c# - 如何使用 EF 迁移将 int ID 列更改为 Guid?

我正在使用EF代码优先方法并想将Id字段更改为guid但似乎无法通过以下错误。这是我的第一次迁移:publicpartialclassCreateDownloadToken:DbMigration{publicoverridevoidUp(){CreateTable("dbo.DownloadTokens",c=>new{Id=c.Int(nullable:false,identity:true),FileId=c.Int(),UserId=c.String(nullable:false,maxLength:128),ValidUntil=c.DateTime(nullable:fal

c# - 集成测试 ASP.NET Core Web API 和 EF Core 时重新配置依赖项

我正在学习本教程IntegrationTestingwithEntityFrameworkCoreandSQLServer我的代码是这样的集成测试类publicclassControllerRequestsShould:IDisposable{privatereadonlyTestServer_server;privatereadonlyHttpClient_client;privatereadonlyYourContext_context;publicControllerRequestsShould(){//ArrangevarserviceProvider=newServiceCo

c# - ASP.NET Core MVC 混合路由/FromBody 模型绑定(bind)和验证

我正在使用ASP.NETCore1.1MVC构建JSONAPI。给定以下模型和操作方法:publicclassTestModel{publicintId{get;set;}[Range(100,999)]publicintRootId{get;set;}[Required,MaxLength(200)]publicstringName{get;set;}publicstringDescription{get;set;}}[HttpPost("/test/{rootId}/echo/{id}")]publicIActionResultTestEcho([FromBody]TestMode

c# - 通过字符串生成EF orderby表达式

这个问题在这里已经有了答案:DynamicLINQOrderByonIEnumerable/IQueryable(22个答案)关闭8个月前。我想通过字符串参数生成表达式,代码如下:privateExpression>Generate(stringorderby){switch(orderby){case"Time":returnt=>t.Time;case"Money":returnt=>t.RewardMoney;default:returnt=>t.Id;}}然后调用它:_context.Items.OrderBy(Generate("Money"));但是编译不了!我将T更改为对

c# - 如何在 ASP.NET Core 中更改 Swagger 的基本 URL

默认情况下,当您在ASP.NETCore项目中启用swagger时,它在url上可用:http://localhost:/swagger/ui我想使用不同的基本url而不是/swagger/ui。我如何/在哪里配置它?我发现对于旧版本你可以配置RootUrl但在ASP.NETCore中没有类似的方法:.EnableSwagger(c=>{c.RootUrl(req=>myCustomBasePath);}); 最佳答案 对于ASP.NETCore2(并使用Swashbuckle.AspNetCore.Swagger-Version4

c# - 在 .NET Core 中注入(inject)

我正在尝试在2016年5月发布的.NETCore中安装Ninject3.3.2。我收到一个错误:依赖项Ninject3.2.2不支持框架.NETCoreApp,版本=v1.0。有没有人遇到过类似的问题,有什么解决办法吗? 最佳答案 Ninject3.3.0于2017年9月26日发布,现在以.NETStandard2.0为目标,因此也可以在.NETCore2.0上运行。从事情的进程来看(请参阅GitHub上的问题/讨论)似乎4.0-beta中的一些更改可能会被恢复。我不希望很快有4.0决赛。因此,我建议使用当前的版本3。

c# - 如何在 EF Core 中实例化 DbContext

我还设置了.net核心项目和数据库上下文。但是由于这个错误,我还不能开始使用dbContext-"thereisnoargumentgiventhatcorrespondstotherequiredformalparameter'options'"Controller:publicIActionResultIndex(){using(vardb=newBlexzWebDb()){}returnView();}数据库上下文代码:publicclassBlexzWebDb:DbContext{publicBlexzWebDb(DbContextOptionsoptions):base(op

c# - 警告 : The referenced component 'Microsoft.Office.Core' could not be found

在构建我的项目之一时,我收到以下警告:Warning3Cannotfindwrapperassemblyfortypelibrary"Microsoft.Office.Core".Warning4Thereferencedcomponent'Microsoft.Office.Core'couldnotbefound.奇怪的是,构建没有错误地失败了。上面的警告似乎是问题所在。在我从WindowsUpdate安装了一些Office2007更新后,就开始出现这种情况。在此之前,它构建的一切都很好。有人遇到过同样的问题吗?关于如何在不修改项目的情况下解决此问题的任何想法?