2

I am new to .Net Core development. I have a model:

public class CoreGoal
{
    [Key]
    public long CoreGoalId { get; set; }
    public string Title { get; set; }
    public string Effect { get; set; }
    public string Target_Audience { get; set; }
    public string Infrastructure { get; set; }

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

    public CoreGoal()
    {

    }
}

And Image model is as following:

public class Image
{
    [Key]
    public long ImagelId { get; set; }
    public string Base64 { get; set; }

    [ForeignKey("CoreGoalId")]
    public long CoreGoalId { get; set; }

    public Image()
    {

    }
}

I am using Repository pattern. My repository:

public interface ICoreGoalRepository
{
    void CreateCoreGoal(CoreGoal coreGoal);

}


public class CoreGoalRepository : ICoreGoalRepository
{
    private readonly WebAPIDataContext _db;

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

    //Find specific
    public CoreGoal Find(long key)
    {
        return _db.CoreGoals.FirstOrDefault(t => t.CoreGoalId == key);
    }

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

}  

And controller:

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

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

    [HttpGet("{id}", Name = "GetCoreGoal")]
    public IActionResult GetById(long id)
    {
        var item = _coreGoalRepository.Find(id);
        if (item == null)
        {
            return NotFound();
        }
        return new ObjectResult(item);
    }

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

        _coreGoalRepository.CreateCoreGoal(item);

        return CreatedAtRoute("GetCoreGoal", new { id = item.CoreGoalId }, item);
    }

}

On POST request for CoreGoal- While creating a new CoreGoal, I would like to convert Image model's Base64 attribute from string to byte[]. I found this (https://adrientorris.github.io/aspnet-core/manage-base64-encoding.html) blogpost, but I am not sure where Am I supposed to write this piece of code.

Can someone help me?

3
  • The question is not clear for me, could you provide more details what you are trying to do, please? Commented Apr 6, 2017 at 9:58
  • So, essentially My CoreGoal can have many Images, that's why I created it as a separate model. Now, Image model has an attribute Base64 whose type is string. When I create a CoreGoal, I also post base64 string along with it, unfortunately while saving to my MySql database this big string is chopped halfway. So someone suggested me that instead of using string, use byte[] as a data type. But If I change it to byte[] my POST request fails with status 400. So, perhaps instead of changing it to byte[] encoding could work. stackoverflow.com/questions/43248760/… Commented Apr 6, 2017 at 10:07
  • Possible duplicate of ASP.Net Core saving base64 string Commented Sep 26, 2017 at 14:09

1 Answer 1

0

Initially you should chage you database model to save you binary image to db (also, it's still not good idea, but let leave it for a now):

public class Image
{
   [Key]
   public long ImagelId { get; set; }
   [NotMapped]
   public string Base64 { get; set; }
   public byte[] Binary {get; set;}

   [ForeignKey("CoreGoalId")]
   public long CoreGoalId { get; set; }

   public Image()
   {

   }
}

next you just should convert your image inside controller:

[HttpPost]
public IActionResult Create([FromBody] CoreGoal item)
{
    if (item == null)
    {
        return BadRequest();
    }
    item.Binary = Convert.FromBase64String(item.Base64);
    _coreGoalRepository.CreateCoreGoal(item);
    return CreatedAtRoute("GetCoreGoal", new { id = item.CoreGoalId }, item);
}

BTW:you code still not good. It's not necessary to use Repository pattern with EF core (https://www.thereformedprogrammer.net/is-the-repository-pattern-useful-with-entity-framework-core/). And you should introduce two model layers: public layer and model layer. You shouldn't expose EF Core contract to outside.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.