Asp.net core ASP.Net核心保存base64字符串

Asp.net core ASP.Net核心保存base64字符串,asp.net-core,asp.net-core-mvc,entity-framework-core,Asp.net Core,Asp.net Core Mvc,Entity Framework Core,我有一个图像模型: public class Image { [Key] public long ImagelId { get; set; } public string base64 { get; set; } } 我使用的方法如下: public class CoreGoal { [Key] public long CoreGoalId { get; set; } [Required] public string Title { ge

我有一个图像模型:

public class Image
{
    [Key]
    public long ImagelId { get; set; }
    public string base64 { get; set; }
}
我使用的方法如下:

public class CoreGoal
{
    [Key]
    public long CoreGoalId { get; set; }
    [Required]
    public string Title { get; set; }

    public virtual ICollection<Image> Images { get; set; }


    public CoreGoal()
    {

    }
}
公共类核心目标
{
[关键]
公共长CoreGoalId{get;set;}
[必需]
公共字符串标题{get;set;}
公共虚拟ICollection映像{get;set;}
公共核心目标()
{
}
}
我正在使用MySql数据库。我打算针对每个CoreGoal存储可能的多个图像作为base64字符串

每当我使用base64字符串发出POST请求时,它都是成功的,但在保存到数据库时,字符串的很大一部分被切碎

我是否使用了错误的数据结构来存储base64,即字符串?这是我的ASP.Net代码中的问题还是MySql的限制

我怎样才能解决这个问题

更新:

我的存储库类:

public class CoreGoalRepository : ICoreGoalRepository
{
    private readonly WebAPIDataContext _db;

    public CoreGoalRepository(WebAPIDataContext db)
    {
        _db = db;
    }

    //Add new
    public void CreateCoreGoal(CoreGoal coreGoal)
    {
        _db.CoreGoals.Add(coreGoal);
        _db.SaveChanges();
    }

    //Get all
    public IEnumerable<CoreGoal> GetAllCoreGoals()
    {
        return _db.CoreGoals
            .Include(coreGoal => coreGoal.Benefits)
            .Include(coreGoal => coreGoal.Images)
            .ToList();
    }
}

public interface ICoreGoalRepository
{
    void CreateCoreGoal(CoreGoal coreGoal);
    IEnumerable<CoreGoal> GetAllCoreGoals();
}
公共类CoreGoalRepository:ICoreGoalRepository
{
私有只读WebAPIDataContext_db;
公共核心目标存储库(WebAPIDataContext数据库)
{
_db=db;
}
//新增
public void CreateCoreGoal(CoreGoal CoreGoal)
{
_db.coregools.Add(coregool);
_db.SaveChanges();
}
//全部
公共IEnumerable GetAllCoreGoals()
{
返回_db.CoreGoals
.Include(coreGoal=>coreGoal.Benefits)
.Include(coregool=>coregool.Images)
.ToList();
}
}
公共接口ICoreGoalRepository
{
void CreateCoreGoal(CoreGoal CoreGoal);
IEnumerable GetAllCoreGoals();
}
我的控制器:

 [Route("api/[controller]")]
    public class CoreGoalController : Controller
    {
        private readonly ICoreGoalRepository _coreGoalRepository;

        //Controller
        public CoreGoalController(ICoreGoalRepository coreGoalRepository) {
            _coreGoalRepository = coreGoalRepository;
        }

        //Get methods
        [HttpGet]
        public IEnumerable<CoreGoal> GetAll()
        {
            return _coreGoalRepository.GetAllCoreGoals();
        }

        //Create
        [HttpPost]
        public IActionResult Create([FromBody] CoreGoal item)
        {
            if (item == null)
            {
                return BadRequest();
            }

            _coreGoalRepository.CreateCoreGoal(item);

            return CreatedAtRoute("GetCoreGoal", new { id = item.CoreGoalId }, item);
        }
     }
[路由(“api/[控制器]”)]
公共类CoreGoalController:控制器
{
私有只读ICoreGoalRepository\u coreGoalRepository;
//控制器
公共CoreGoalController(ICoreGoalRepository coreGoalRepository){
_coreGoalRepository=coreGoalRepository;
}
//获取方法
[HttpGet]
公共IEnumerable GetAll()
{
return _coreGoalRepository.GetAllCoreGoals();
}
//创造
[HttpPost]
公共IActionResult创建([FromBody]核心目标项)
{
如果(项==null)
{
返回请求();
}
_coreGoalRepository.CreateCoreGoal(项目);
返回CreatedAtRoute(“GetCoreGoal”,新的{id=item.CoreGoalId},item);
}
}

当您发出post请求时,还将返回作为JSON创建的对象,以便您可以查看URL,或者在post中放置断点并查看base64字符串的值


如果URL正常,那么它就是数据库,因此您需要手动设置URL的最大大小,属性[StringLength(length)]

可能会被数据库截断。检查数据库中字符串的长度与字段的长度。您要保存的图像有多大?它实际上节省了多少呢?ASP.NETCore不会将任何内容保存到数据库中。这是一些其他的技术,看起来你正在使用EF 6或EF Core。来自FluentMigrator这样的迁移器?它是现有的数据库吗?在确定诊断之前,您需要回答这些问题。因为您不确定是否要使用数据模型,我建议您在以下两者之间进行选择:1。如果要粘贴字符串,请将图像的路径引用存储在AspNet应用程序可访问的文件夹中。2.在记录中使用BLOB字段,并让mySql处理它。然后可以在后端或前端实现base64序列化/反序列化。如果你用谷歌搜索你的问题,有很多页面(还有这个问题的副本),也许你会找到更多的模式。我会用
varbinary(max)