小编Rob*_*Coy的帖子

EntityFramework Core - 复制实体并将其放回数据库

是否有最佳实践来复制实体,根据用户输入对其进行一些更改,然后将其重新插入数据库?

其他一些Stackoverflow线程已经提到过,即使数据库中存在相同的主键,EF也会为您处理插入新对象,但我不太确定EF Core是如何处理它的.每当我尝试复制一个对象时,我都会收到错误

Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF
Run Code Online (Sandbox Code Playgroud)

基本上我只需要一种简洁的方法来复制对象,根据用户输入对其进行一些更改,然后将该副本插回到数据库中,并使Id自动递增.有没有最佳实践或简单的方法,而无需手动将属性设置为null或空?

编辑:从数据库中检索对象的示例代码:

    public Incident GetIncidentByIdForCloning(int id)
    {
        try
        {
            return _context.Incident.Single(i => i.IncidentId == id);
        }
        catch
        {
            return null;
        }
    }
Run Code Online (Sandbox Code Playgroud)

检索对象后的代码(因为某些字段是自动生成的,如RowVersion,它是一个时间戳):

public IActionResult Clone([FromBody]Incident Incident)
    {
        var incidentToCopy = _incidentService.IncidentRepository.GetIncidentByIdForCloning(Incident.IncidentId);
        incidentToCopy.IncidentTrackingRefId = _incidentService.IncidentRepository.GetNextIdForIncidentCategoryAndType(
            Incident.IncidentCategoryLookupTableId, Incident.IncidentTypeLookupTableId).GetValueOrDefault(0);
        incidentToCopy.RowVersion = null;
        incidentToCopy.IncidentId = 0; //This will fail with or without this line, this was more of a test to see if manually setting …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc entity-framework entity-framework-core asp.net-core

7
推荐指数
2
解决办法
4829
查看次数

服务工作者在React应用程序中刷新后导致空白页

我有一个部署到Azure的Node应用程序,其中一个测试分支通向一个临时实例,一个master分支指向prod部署。我的应用程序在本地应用程序的所有实例上都能正常工作,但是生产和暂存存在以下问题:如果它们已经被缓存,则无法加载,并且在刷新后将显示为空白,最后在重置缓存后可以正常工作。

每当我在生产环境中刷新页面时,它都是空白。服务工作者正在运行(我可以在chrome serviceworkers-internal工具中看到它),但是该页面从未加载。生成的文件引用正确。您可以在此处看到发生了什么事的示例:Live Site,然后还可以看到测试站点,该站点也因部署了完全相同的代码而失败:Test Site

整个ServiceWorker实现都是开箱即用的create-react-app。我花了几个小时试图在react-boilerplate和create-react-app存储库下的各种GitHub问题中跟踪此错误,但除了限制页面缓存外,它们都没有真正发挥作用,而我尝试这样做毫无用处与:

Index.html

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="0"/>
Run Code Online (Sandbox Code Playgroud)

您可以在托管所有这些代码的存储库中找到对任何代码有疑问的任何代码。

我只是想用React / Node弄湿我的脚,所以我陷入困境,在不完全取消ServiceWorker注册的情况下,真不知道如何解决这个问题。

编辑:我从index.js文件中完全删除了ServiceWorker代码,现在重新加载视域而没有任何问题。我需要完成一个步骤来使ServiceWorker从缓存或其他内容正确地重新加载页面吗?

azure node.js express socket.io service-worker

6
推荐指数
1
解决办法
823
查看次数

核心应用程序(针对.NET Framework) - IIS发布错误"无法加载文件或程序集System.Runtime"

我正在尝试将我在.NET Core中编写的应用程序移植到.NET Core(Targeting .NET Framework),因为无法模拟Core [来自我以前的SO ].

我创建了应用程序并移植了所有的Views,Controller和Models,并按照我的需要实现了WebClient.它在我的IIS Express本地运行完全正常,但是当我发布到IIS时,我得到错误:

FileLoadException: Could not load file or assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Run Code Online (Sandbox Code Playgroud)

有几个StackOverflow线程和解决方案,但它们都是为带有project.json设置的.NET Core 1.0.1而设计的,并且它们不能很好地转换为csproj.有没有人遇到此错误?

完整堆栈跟踪:

System.IO.FileLoadException: Could not load file or assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'System.Runtime, Version=4.1.0.0, Culture=neutral, …
Run Code Online (Sandbox Code Playgroud)

asp.net asp.net-core

5
推荐指数
0
解决办法
249
查看次数

HttpResponseMessage不返回ByteArrayContent-ASP.NET Core

我将文件存储在数据库中,需要Web API才能返回。数据库调用正确地返回了正确的字节数组(Breakpoint显示该数组的长度约为67000,这是正确的),但是当我调用Web API时,我从没有在响应中得到该内容。我试过使用MemoryStream和ByteArrayContent,但都没有给我应该得到的结果。我试过从Postman和我的MVC应用程序中调用,但是都没有返回字节数组,只是返回了带有headers / success / etc的基本响应信息。

public HttpResponseMessage GetFile(int id)
{
    var fileToDownload = getFileFromDatabase(id);
    if (fileToDownload == null)
    {
        return new HttpResponseMessage(HttpStatusCode.BadRequest);
    }
    var response = new HttpResponseMessage(HttpStatusCode.OK);
    response.Content = new ByteArrayContent(fileToDownload.FileData); //FileData is just a byte[] property in this class
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
    return response;
}
Run Code Online (Sandbox Code Playgroud)

我得到的典型响应(在任何地方都找不到字节内容):

{
  "version": {
    "major": 1,
    "minor": 1,
    "build": -1,
    "revision": -1,
    "majorRevision": -1,
    "minorRevision": -1
  },
  "content": {
    "headers": [
      {
        "key": "Content-Disposition",
        "value": [ …
Run Code Online (Sandbox Code Playgroud)

asp.net asp.net-mvc asp.net-core asp.net-core-webapi

3
推荐指数
1
解决办法
3193
查看次数