繁体   English   中英

ASP.Net Core-使用SQLite-显示数据,但不使用Entity Framework保存

[英]ASP.Net Core - Using SQLite - Data showing, but not saving with Entity Framework

我正在尝试在ASP.Net CoreEntityFramework Core中使用SQLite

当我处于调试模式(使用Visual Studio)时,一切正常。

当我发布它并尝试接收数据时,它正在运行。

数据读取控制器是这样的-

[HttpGet]
public async Task<IActionResult> Get(int start=0,int end=0)
{
    var bulletins = await _context.Bulletin
        .Include(u => u.Descriptions)
        .Include(u => u.Images)
        .ToArrayAsync();

    var response = bulletins.Select(u => new
    {
        Id = u.Id,
        UserId = u.UserId,
        Title = u.Title,
        Descriptions = u.Descriptions.Select(p => p.Text),
        Images = u.Images.Select(p => p.Name),
        BaseUrl = "localhost/uploads/"
    });

    return Ok(response);
}

但是当我试图保存这样的数据时-

[HttpPost]
public IActionResult Insert(BulletinViewModel data, ICollection<IFormFile> image)
{
    List<Description> bulletinDescription = new List<Description>();

    foreach (var description in data.text)
    {
        bulletinDescription.Add(new Description{ Text = description });
    }

    //string filename1 = _environment.WebRootPath;

    List<Image> bulletinImages = new List<Image>();
    string path = Directory.GetCurrentDirectory();

    foreach (var file in image)
    {
        string fileName = Path.GetFileNameWithoutExtension(file.FileName);
        string extention = Path.GetExtension(file.FileName);

        var filename = ( data.user_id
                        + "_"
                        + Guid.NewGuid().ToString()
                        + "_"
                        + fileName
                        +extention).Trim('"');

        bulletinImages.Add(new Image { Name = filename });

        var serverFile = uploadDirectory + $@"/{filename}";
        //file.Length;
        using (FileStream fileStream = System.IO.File.Create(serverFile))
        {
            file.CopyTo(fileStream);
            fileStream.Flush();
        }
    }

    Bulletin bulletin = new Bulletin {
        UserId = data.user_id,
        Title = data.title,
        Descriptions = bulletinDescription,
        Images = bulletinImages
    };

    _context.Bulletin.Add(bulletin);
    _context.SaveChanges();

    return Ok(bulletin);
}

在调试模式下(使用Visual Studio) 工作时,不能在生产中工作(发布后

我收到此错误 (此处提供了总日志)-

   Hosting environment: Production
   Content root path: D:\Publish
   Now listening on: http://localhost:5000
   Application started. Press Ctrl+C to shut down.
   info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
         Request starting HTTP/1.1 GET http://localhost:5000/api/bulletin/get
   info: Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory[1]
         Executed DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
         PRAGMA foreign_keys=ON;
   info: Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory[1]
         Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
         PRAGMA foreign_keys=ON;
   info: Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory[1]
         Executed DbCommand (3ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
         SELECT COUNT(*) FROM "sqlite_master" WHERE "type" = 'table' AND "rootpage" IS NOT NULL;
   info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
         Executing action method BugTracker.api.BulletinController.Get (BugTracker) with arguments (0, 0) - ModelState is Valid
   info: Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory[1]
         Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
         PRAGMA foreign_keys=ON;
   info: Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory[1]
         Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
         SELECT "u"."Id", "u"."Title", "u"."UserId"
         FROM "Bulletin" AS "u"
         ORDER BY "u"."Id"
   info: Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory[1]
         Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
         SELECT "i"."Id", "i"."BulletinId", "i"."Name"
         FROM "Image" AS "i"
         WHERE EXISTS (
             SELECT 1
             FROM "Bulletin" AS "u"
             WHERE "i"."BulletinId" = "u"."Id")
         ORDER BY "i"."BulletinId"
   info: Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory[1]
         Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
         SELECT "d"."Id", "d"."BulletinId", "d"."Text"
         FROM "Description" AS "d"
         WHERE EXISTS (
             SELECT 1
             FROM "Bulletin" AS "u"
             WHERE "d"."BulletinId" = "u"."Id")
         ORDER BY "d"."BulletinId"
   info: Microsoft.AspNetCore.Mvc.Internal.ObjectResultExecutor[1]
         Executing ObjectResult, writing value Microsoft.AspNetCore.Mvc.ControllerContext.
   info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
         Executed action BugTracker.api.BulletinController.Get (BugTracker) in 4224.5672ms
   info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
         Request finished in 5902.4455ms 200 application/json; charset=utf-8
   info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
         Request starting HTTP/1.1 POST http://localhost:5000/api/bulletin/insert multipart/form-data; boundary=----WebKitFormBoundaryGALOJJbtKdvvVFah 2532310
   info: Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory[1]
         Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
         PRAGMA foreign_keys=ON;
   info: Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory[1]
         Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
         PRAGMA foreign_keys=ON;
   info: Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory[1]
         Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
         SELECT COUNT(*) FROM "sqlite_master" WHERE "type" = 'table' AND "rootpage" IS NOT NULL;
   info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
         Executing action method BugTracker.api.BulletinController.Insert (BugTracker) with arguments (BugTracker.Model.ViewModels.BulletinViewModel, System.Collections.Generic.List`1[Microsoft.AspNetCore.Http.IFormFile]) - ModelState is Valid
   info: Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory[1]
         Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
         PRAGMA foreign_keys=ON;
   info: Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory[1]
         Executed DbCommand (4ms) [Parameters=[@p0='?', @p1='?'], CommandType='Text', CommandTimeout='30']
         INSERT INTO "Bulletin" ("Title", "UserId")
         VALUES (@p0, @p1);
         SELECT "Id"
         FROM "Bulletin"
         WHERE changes() = 1 AND "Id" = last_insert_rowid();
   info: Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory[1]
         Executed DbCommand (0ms) [Parameters=[@p2='?', @p3='?'], CommandType='Text', CommandTimeout='30']
         INSERT INTO "Description" ("BulletinId", "Text")
         VALUES (@p2, @p3);
         SELECT "Id"
         FROM "Description"
         WHERE changes() = 1 AND "Id" = last_insert_rowid();
   info: Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory[1]
         Executed DbCommand (0ms) [Parameters=[@p0='?', @p1='?'], CommandType='Text', CommandTimeout='30']
         INSERT INTO "Description" ("BulletinId", "Text")
         VALUES (@p0, @p1);
         SELECT "Id"
         FROM "Description"
         WHERE changes() = 1 AND "Id" = last_insert_rowid();
   info: Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory[1]
         Executed DbCommand (0ms) [Parameters=[@p0='?', @p1='?'], CommandType='Text', CommandTimeout='30']
         INSERT INTO "Image" ("BulletinId", "Name")
         VALUES (@p0, @p1);
         SELECT "Id"
         FROM "Image"
         WHERE changes() = 1 AND "Id" = last_insert_rowid();
   info: Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory[1]
         Executed DbCommand (0ms) [Parameters=[@p0='?', @p1='?'], CommandType='Text', CommandTimeout='30']
         INSERT INTO "Image" ("BulletinId", "Name")
         VALUES (@p0, @p1);
         SELECT "Id"
         FROM "Image"
         WHERE changes() = 1 AND "Id" = last_insert_rowid();
   info: Microsoft.AspNetCore.Mvc.Internal.ObjectResultExecutor[1]
         Executing ObjectResult, writing value Microsoft.AspNetCore.Mvc.ControllerContext.
   fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]
         An unhandled exception has occurred: Self referencing loop detected for property 'bulletin' with type 'BugTracker.DbModels.Mcp.Bulletin'. Path 'descriptions[0]'.
   Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property 'bulletin' with type 'BugTracker.DbModels.Mcp.Bulletin'. Path 'descriptions[0]'.
      at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer, Object value, JsonProperty property, JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty)
      at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)
      at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
      at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
      at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
      at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
      at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
      at Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter.WriteObject(TextWriter writer, Object value)
      at Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter.<WriteResponseBodyAsync>d__9.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
      at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeResultAsync>d__32.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
      at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeResultFilterAsync>d__31.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAllResultFiltersAsync>d__29.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
      at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeResourceFilterAsync>d__23.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAsync>d__18.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
      at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
      at Microsoft.ApplicationInsights.AspNetCore.ExceptionTrackingMiddleware.<Invoke>d__4.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
      at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>d__6.MoveNext()
   warn: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]
         The response has already started, the error handler will not be executed.
   fail: Microsoft.AspNetCore.Server.Kestrel[13]
         Connection id "0HKUCLKEV7G2Q": An unhandled exception was thrown by the application.
   Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property 'bulletin' with type 'BugTracker.DbModels.Mcp.Bulletin'. Path 'descriptions[0]'.
      at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer, Object value, JsonProperty property, JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty)
      at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)
      at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
      at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
      at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
      at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
      at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
      at Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter.WriteObject(TextWriter writer, Object value)
      at Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter.<WriteResponseBodyAsync>d__9.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
      at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeResultAsync>d__32.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
      at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeResultFilterAsync>d__31.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAllResultFiltersAsync>d__29.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
      at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeResourceFilterAsync>d__23.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAsync>d__18.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
      at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
      at Microsoft.ApplicationInsights.AspNetCore.ExceptionTrackingMiddleware.<Invoke>d__4.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
      at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>d__6.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>d__6.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
      at Microsoft.ApplicationInsights.AspNetCore.RequestTrackingMiddleware.<Invoke>d__4.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
      at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.<Invoke>d__3.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
      at Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.<Invoke>d__3.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
      at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame`1.<RequestProcessingAsync>d__2.MoveNext()
   info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
         Request finished in 12477.1332ms 200 application/json; charset=utf-8

谁能帮忙吗?

在此先感谢您的帮助。

与错误有关的部分是

发生未处理的异常:为类型为“ BugTracker.DbModels.Mcp.Bulletin”的属性“ bulletin”检测到自引用循环。 路径“描述[0]”。 Newtonsoft.Json.JsonSerializationException:为类型为“ BugTracker.DbModels.Mcp.Bulletin”的属性“ bulletin”检测到自引用循环。 路径“描述[0]”。 在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer,Object value,JsonProperty property,JsonContract contract,JsonContainerContract containerContract,JsonProperty

因此,在模型的某个地方,您有一个引用循环,其中一个属性引用另一个模型类,而该模型类对包含原始属性的类进行引用。

我建议只保存更新的项目,而不要尝试保存整个树。 如果还更改了多个引用,则需要制定一个过程,在该过程中,引用循环不会成批保存,而是作为第二个保存动作添加。

感谢Adem Caglin的引用

我已经做到了-

services.AddMvc().AddJsonOptions(options => {
        options.SerializerSettings.ReferenceLoopHandling =  ReferenceLoopHandling.Ignore;
    });

一切都OK :)。

这里这里可以找到更多。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM