我在AspNetCore OData-webapp中使用NodaTime作为DateTime-API。一切都按预期进行,直到序列化为止。这是我的entityBase。
public class DbEntity
{
public Instant CreateDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的Startup-ConfigureServices方法:
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddJsonOptions(options => options.SerializerSettings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb));
services.AddDbContext<DefaultContext>(
options => options
.UseLazyLoadingProxies()
.UseNpgsql(Configuration.GetConnectionString("someString"), o => o.UseNodaTime()));
services.AddTransient<ICrudEditable, CrudEditable>();
services.AddOData();
Run Code Online (Sandbox Code Playgroud)
我Microsoft.AspNetCore.OData 7.0.1 NodaTime 2.4.0与NodaTime.Serialization.JsonNet 2.0.0和一起使用Version Newtonsoft.Json 11.0.2。
动作:
// GET api/values
[HttpGet]
public ActionResult Get ( )
{
return Ok(new DbEntity { CreateDate = SystemClock.Instance.GetCurrentInstant() });
}
Run Code Online (Sandbox Code Playgroud)
到目前为止一切都很好。这是默认值控制器(源自ControllerBase)的操作。但是,如果我在操作从Controller派生自ODataController的情况下进行以下调用,则CreateDate为空:
[EnableQuery]
[ODataRoute("entity")]
public IActionResult Get()
{
return Ok(new DbEntity { CreateDate = SystemClock.Instance.GetCurrentInstant() }); …Run Code Online (Sandbox Code Playgroud) 我遇到了问题,我的AspNetCore.App-metapackage引用了较低版本的EntityFrameworkCore(2.1.2),而不是EfCore提供程序包(NpgSql,引用2.1.3).结果是警告MSB3277(这是问题).这个快速修正是接受的答案.
另一个答案指出,我使用的是较低的Microsoft.AspNetCore.App软件包(当时为2.1.1),而不是上一个稳定版本(2.1.4).更改包版本是不可能的(见下图).
我在类库项目中遇到了与Microsoft.NETCore.App相同的问题
我甚至没有注意到我使用了比现有更旧的元数据包.直到今天,我总是检查,如果NuGet包管理器中有任何更新.我使用了默认的项目模板,并且始终安装了最新的.NetCore SDK,相信这已经足够了.不是.
在研究了这个问题之后,我发现,我可以强制我的项目使用包管理器控制台(Install-Package Microsoft.NETCore.App -Version 2.1.4或Install-Package Microsoft.AspNetCore.App -Version 2.1.4)来使用特定的.NETCore.App或AspNetCore.App元数据包.
在那个命令后我有一个构建错误(NETSDK1061: The project was restored using Microsoft.NETCore.App version 2.1.4, but with current settings, version 2.1.0 would be used instead. To resolve this issue, make sure the same settings are used for restore and for subsequent operations such as build or publish.).
我们想要开发一个新的 ASP.NET Web 应用程序,如果可能的话,我们想要采用 ASP.NET Core。这个愿望的原因之一是,我们爱上了 ASP.NET Core Identity。但一个问题是,Identity 依赖于 Entity Framework,而 Entity Framework Core 仍然有一些我们\xc2\xb4ll 需要的未实现的功能(https://github.com/aspnet/EntityFramework/wiki/Roadmap)。
\n\nI\xc2\xb4ve 发现一篇文章,其中作者提供了将 Entity Framework 6 与 ASP.NET Core 一起使用的可能性(https://learn.microsoft.com/en-us/aspnet/core/data/entity-framework-6)。使用 EF 6 和 ASP.NET Core 的推荐方法是,创建一个针对 .NET Framework 4.6.something 的新 dll,并将所有 EF 内容放入其中。由于这种方法是我们一般数据处理的计划,因此也需要以某种方式访问身份数据。并且有许多专门用于 Identity 的 EF 函数(例如 AspNetCore.Identiy.EntityFrameworkCore),使授权/身份验证工作更好、更容易、更快、更方便 - 无论如何。
\n\n但是,使用不同的框架(或者更糟糕的是一个框架的不同版本)来访问同一个数据库,或者使用不同的技术在两个不同的地方两次访问相同的数据,并不是我们所想到的那种一劳永逸的方式。
\n\n也许我完全被难住了,这很清楚,但我现在确实没有一个干净的方法或可接受的方法来解决这个问题。
\n\n有任何想法吗?
\n不幸的是,ef core不支持TPC模式,但我们需要这种行为.我写了一个名为IBase的接口,每个实体都实现了这个接口:
public interface IBase
{
Guid Id { get; set; }
[Column(TypeName = "datetime2")]
DateTime CreateDate { get; set; }
[Required]
[StringLength(255)]
string CreateUser { get; set; }
bool Deleted { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想摆脱Annotations来改为使用Fluent API配置.但我有大约20个不同的实体和7个Base-Values,我不想一遍又一遍地做出相同的配置:
modelBuilder.Entity<SomeEntity>()
.Property(e => e.CreateDate)
.HasColumnType("datetime2(2)")
.IsRequired();
Run Code Online (Sandbox Code Playgroud)
任何想法如何为所有实施IBase的实体配置一次Base-Property?
c# entity-framework entity-framework-core ef-fluent-api .net-core
我已经尝试了很多,但我找不到我真正要找的东西。这是我的情况:我有一个带有导航属性和视图模型的 EF-Core 实体:
public class SomeEntity
{
public Guid Id { get; set; }
public virtual NestedObject NestedObject { get; set; }
public DateTime Created { get; set; }
public DateTime Modified { get; set; }
}
public class SomeEntityViewModel
{
public Guid Id { get; set; }
public string NestedObjectStringValue { get; set; }
public int NestedValueIntValue { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的 CreateMap,即使没有设置 NestedObject-Property,它也会创建一个新的 NestedObject(条件似乎不适用于此处):
CreateMap<SomeEntityViewModel, SomeEntity>(MemberList.Source)
.ForPath(m => m.NestedObject.StringValue, opt =>
{
opt.Condition(s => s.Destination.NestedObject != null); …Run Code Online (Sandbox Code Playgroud) 我有一个带有多个补丁操作的 AspNetCore-WebApi-Project,它在 Core 2.2 中运行良好。迁移到 Core 3 后[FromBody] JsonPatchDocument<T>为空。我的获取/发布方法仍然按预期运行。
这是我的 Startup 的一部分:
services.AddDbContext<MyContext>(options => options
.UseLazyLoadingProxies()
.UseNpgsql(Configuration.GetConnectionString("MyConnectionString"),
opt => opt.UseNodaTime()));
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My-API", Version = "v1" });
});
services.AddControllers()
.AddNewtonsoftJson();
Run Code Online (Sandbox Code Playgroud)
这是我的行动:
[HttpPatch("{id}")]
public async Task<IActionResult> Patch(Guid id,
[FromBody] JsonPatchDocument<MyViewModel> patchDocument)
{
await this.service.HandlePatchAsync(id, patchDocument);
return NoContent();
}
Run Code Online (Sandbox Code Playgroud)
这是正文内容:
[
{
"op": "replace",
"path": "/name",
"value": "New Name"
},
{
"op": "replace",
"path": "/country",
"value": "Germany"
}
]
Run Code Online (Sandbox Code Playgroud)
有谁知道这里出了什么问题?
.net-core ×6
asp.net-core ×5
c# ×4
automapper ×1
json-patch ×1
json.net ×1
nodatime ×1
npgsql ×1
odata ×1