使用Entity Framework Core

时间:2016-10-21 21:42:10

标签: c# mysql asp.net-core-mvc entity-framework-core

使用MariaDb实例数据库(MySql的分支)并使用Entity Framework Core,给我一个我以前从未体验过的问题,因此这篇帖子因为我不知道发生了什么,并希望有些你们之前可能会经历过这种情况。

问题

采取以下方案。我们有一个看起来像这样的表

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

使用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();
    }
}

正如我以前从常规的ASP.NET MVC应用程序中习惯的那样,我希望这个工作得很好。不过这是一个问题。

在我的数据可视化工具中查看数据(在本例中使用HeidiSQL)我可以看到字符串Content被缩短了。我认为这可能只是HeidiSQL中的设置,不显示完整的字符串,只显示x个字符。现在,如果我从数据库中退出此条目,我可以看到字符串IS实际上只有x个字符长,其中我的初始字符串可能是x * 5

这给我带来了一些问题,因为缩短了Content字符串,但是如果用户上传了我可能想要转换为base64字符串的图像。这些字符串往往很长(当然取决于数据),这也会缩短。

正如本文开头所提到的:我完全不知道发生了什么。我以前从未遇到过这个奇怪的问题,而且我似乎无法通过谷歌的方式找到解决方案。

任何人都可以伸出援助之手向我解释发生了什么事吗?

字符串内容示例

保存到db之前(我希望保存的实际字符串,在textarea前端输入) 490个字符

  

Lorem ipsum dolor sit amet,ne per virtute insolens menandri,in utamur diceret menandri,fastidii petentium explicari et cum。 Ex saperet definiebas quaerendum pri。 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。在pri ancillae ponderum,ius是一个vidit dicant laudem。 Ei usu corpora officiis principes,offendit percipitur eos et,qui ne consetetur concludaturque。

保存到数据库后(保存后检索的实际字符串) 252个字符

  

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

EXTRA

看起来它实际上保存在数据库中的235-260个字符左右。休息时间就被废弃了。我有几个案例(238个字符,243个,253个等)

新闻模型

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

enter image description here

1 个答案:

答案 0 :(得分:3)

临时解决方案

目前看来,ASP.NET MVC Core的官方MySql.EntityFramework.Core库工作不正常,这是一个错误。切换到社区版本:Pomelo.EntitytFramework.Core后,一切都像魅力一样。这个为我做了。

您可以找到Pomelo存储库here