简体   繁体   English

使用Entity Framework Core不会在MySql实例中插入所有数据

[英]All data is not inserted in MySql instance using Entity Framework Core

Using a MariaDb instance database (fork of MySql) and using Entity Framework Core, give me a problem I've never experienced before, hence this post as I have no idea what is going on, and hoping that some of you guys might've experienced this before. 使用MariaDb实例数据库(MySql的fork)并使用Entity Framework Core,给我一个我以前从未经历过的问题,因此这篇文章因为我不知道发生了什么,并希望你们中的一些人可能会以前经历过这个。

Problem 问题

Take the following scenario. 采取以下方案。 We have a table that look like this 我们有一个看起来像这样的表

News
-------------------------------------
| Id          | int      | not null |
| Title       | text     | not null |
| Content     | longtext | not null |
| Base64Image | blob     | null     |
-------------------------------------

Using C#, as my web application is build in ASP.NET Core MVC, I create a new instance of News object, just like so: 使用C#,因为我的Web应用程序是在ASP.NET Core MVC中构建的,所以我创建了一个新的News对象实例,就像这样:

public IActionResult New (NewsViewModel model)
{
    using (var db = new DatabaseContext())
    {
        var news = new News()
        {
            Title = model.Title,
            Content = model.Content
        };

        db.News.Add(news);
        db.SaveChanges();
    }
}

Exactly as I'm used to from regular ASP.NET MVC applications, and I'd expect this to work just fine. 正如我以前习惯的常规ASP.NET MVC应用程序一样,我希望它能正常工作。 Here's the catch though. 这是抓住了。

Viewing the data within my data visualizer (using HeidiSQL in this case) I can see that the string Content is shortened. 在我的数据可视化工具中查看数据(在这种情况下使用HeidiSQL)我可以看到字符串Content缩短了。 I thought that this might just be a settings in HeidiSQL to not show the full string but only x amount of characters. 我认为这可能只是HeidiSQL中的设置,不显示完整的字符串,只显示x个字符。 Now, if I pull this entry back out from the database, I can see that the string IS actually only x amount of characters long, where as my initial string might be x * 5 . 现在,如果我从数据库中取回此条目,我可以看到字符串IS实际上只有x个字符长,其中我的初始字符串可能是x * 5

This gives me several problems, as the Content string is shortened, but also if the user uploads an image that I might want to convert to a base64 string. 这给我带来了一些问题,因为缩短了Content字符串,但是如果用户上传了我可能想要转换为base64字符串的图像。 These strings tend to be long (depending on the data of course) and this will ALSO get shortened. 这些字符串往往很长(当然取决于数据),这也会缩短。

As mentioned in the very beginning of this post: I have absolutely no idea what is going on. 正如本文开头所提到的:我完全不知道发生了什么。 I've never encountered this strange issue before, and I can't seem to google my way to a solution. 我以前从未遇到过这个奇怪的问题,而且我似乎无法通过google方式找到解决方案。

Can anyone lend a hand and explain to me what is going on? 任何人都可以伸出援助之手向我解释发生了什么事吗?

Examples of string content 字符串内容的示例

Before saving to db (actual string I want to save, entered in a textarea frontend) 490 characters 在保存到db之前(我希望保存的实际字符串,在textarea前端中输入) 490个字符

Lorem ipsum dolor sit amet, ne per virtute insolens menandri, in cum utamur diceret menandri, fastidii petentium explicari et cum. Lorem ipsum dolor sit amet,ne per virtute insolens menandri,in utamur diceret menandri,fastidii petentium explicari et cum。 Ex saperet definiebas quaerendum pri. Ex saperet definiebas quaerendum pri。 Ei sed alii alterum alienum, mei graeco meliore pertinax eu, nec cu propriae molestie. Ei sed alii alterum alienum,mei graeco meliore pertinax eu,nec cu propriae molestie。 Id eum zril timeam, at error gloriatur consectetuer est, et vim ceteros assueverit. Id eum zril timeam,at error gloriatur consectetuer est,et vim ceteros assueverit。 At pri ancillae ponderum, ius an vidit dicant laudem. 在pri ancillae ponderum,ius是一个vidit dicant laudem。 Ei usu corpora officiis principes, offendit percipitur eos et, qui ne consetetur concludaturque. Ei usu corpora officiis principes,offendit percipitur eos et,qui ne consetetur concludaturque。

After saving to the database (actual string as retrieved post save) 252 characters 保存到数据库后(保存后检索的实际字符串) 252个字符

Lorem ipsum dolor sit amet, ne per virtute insolens menandri, in cum utamur diceret menandri, fastidii petentium explicari et cum. Lorem ipsum dolor sit amet,ne per virtute insolens menandri,in utamur diceret menandri,fastidii petentium explicari et cum。 Ex saperet definiebas quaerendum pri. Ex saperet definiebas quaerendum pri。 Ei sed alii alterum alienum, mei graeco meliore pertinax eu, nec cu propriae molest Ei sed alii alterum alienum,mei graeco meliore pertinax eu,nec cu propriae molest

EXTRA 额外

It looks like it's consistently around 235-260 characters that is actually saved to the database. 它看起来一直保持在235-260个字符,实际上保存到数据库中。 Rest is just scrapped away. 休息时间就被废弃了。 I've had several cases (238 characters, 243, 253, etc.) 我有几个案例(238个字符,243个,253个等)

NEWS MODEL 新闻模型

public class News
{
    public int Id { get; set; }

    [Display(Name = "Title", ResourceType = typeof(Resources.General))]
    public string Title { get; set; }

    [Display(Name = "Content", ResourceType = typeof(Resources.General))]
    public string Content { get; set; }

    public DateTime CreateDate { get; set; }

    [Display(Name = "ThumbnailImage", Description = "NewsThumbnailImageDescription", ResourceType = typeof(Resources.General))]
    public string Base64ThumbnailImage { get; set; }

    [Display(Name = "HeaderImage", Description = "NewsHeaderImageDescription", ResourceType = typeof(Resources.General))]
    public string Base64HeaderImage { get; set; }

    public string UserProfileUserName { get; set; }

    public UserProfile UserProfile { get; set; }
}

DB TABLE DB TABLE

在此输入图像描述

TEMPORARY SOLUTION 临时解决方案

As it currently stands, the official MySql.EntityFramework.Core library for ASP.NET MVC Core is not working correctly, and this is a bug. 目前的情况是,ASP.NET MVC Core的官方MySql.EntityFramework.Core库无法正常工作,这是一个错误。 After switching to a community version: Pomelo.EntitytFramework.Core , everything works like a charm. 切换到社区版本: Pomelo.EntitytFramework.Core ,一切都像魅力一样。 This one did it for me. 这个为我做了。

You can find the Pomelo repository here 您可以在这里找到Pomelo存储库

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

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