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.
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:
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. 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. I thought that this might just be a settings in HeidiSQL to not show the full string but only x
amount of characters. 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
.
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. 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.
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
Lorem ipsum dolor sit amet, ne per virtute insolens menandri, in cum 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. At pri ancillae ponderum, ius an vidit dicant laudem. 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
Lorem ipsum dolor sit amet, ne per virtute insolens menandri, in cum 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
It looks like it's consistently around 235-260 characters that is actually saved to the database. Rest is just scrapped away. I've had several cases (238 characters, 243, 253, etc.)
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
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. After switching to a community version: Pomelo.EntitytFramework.Core
, everything works like a charm. This one did it for me.
You can find the Pomelo repository here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With