0

I want to make an API that can receive any number of child IDs. and loop it in database adding 1 row for each child IDs. I tried making the CategoryChildId a list or array but it says Entity framework does not accept primitive types.

How can i achieve this?

for example when i receive a json like this:

"CategoryParentID": "4",
"CategoryChildID": [
    {
      5,6,7
    }
  ],

It will store it in database like this.

enter image description here

I only have the code for adding single child at a time.

The service for the controller:

 public async Task<ServiceResultWithoutBaseEntity<CategoryRollup>> Add(CategoryRollupViewModel newItem)
{
    var result = new ServiceResultWithoutBaseEntity<CategoryRollup>();
    result.Errors.AddRange(Validate(newItem));

    if (result.HasErrors)
        return result;

    var item = newItem.MapToEntity(new CategoryRollup());

    _context.CategoryRollups.Add(item);
    await _context.SaveChangesAsync();

    result.EntityResult = item;

    return result;
}

model:

    public class CategoryRollup
{
    [Key]
    public int ID { get; set; }

    public int CategoryChildID { get; set; }

    public int CategoryParentID { get; set; }
}

view model:

public class CategoryRollupViewModel
{
    [Key]
    public int ID { get; set; }

    public int CategoryChildID { get; set; }

    public int CategoryParentID { get; set; }

}

helper:

  public static class CategoryRollupHelper
{
    public static CategoryRollupViewModel MapFromEntity(this CategoryRollup source)
    {
        if (source == null)
            return null;

        var result = new CategoryRollupViewModel
        {
            CategoryChildID = source.CategoryChildID,
            CategoryParentID = source.CategoryParentID,
        };

        return result;
    }

    public static CategoryRollup MapToEntity(this CategoryRollupViewModel source, CategoryRollup entity)
    {
        if (source == null || entity == null)
            return null;

        entity.CategoryChildID = source.CategoryChildID;
        entity.CategoryParentID = source.CategoryParentID;

        return entity;
    }
}
4
  • Can you show your code for adding single child Commented Jun 9, 2019 at 12:37
  • Make your API accept an object which has one of its attributes as a list of integer, then pass you Json to it. I believe you know how to loop the list and under into the db. Commented Jun 9, 2019 at 12:42
  • @Bosco i tried that but it is saying Entity framework does not accept primitive types. Commented Jun 9, 2019 at 12:46
  • Look at this stackoverflow.com/questions/3227525/… and any other Entity framework question related to "multiple inserts" Commented Jun 9, 2019 at 12:57

2 Answers 2

0

The JSON example is not valid. The CategoryChildID must hold an array of integers (drop the {}).

Lets suppose the JSON has the following structure:

"CategoryParentID": "4",
"CategoryChildID": [5,6,7]

That means your view model should contain two properties (create new class)

public class CategoryRollupViewModelV2
{

    public List<int> CategoryChildID { get; set; }

    public int CategoryParentID { get; set; }

}

Change your controller code to:

public async Task<ServiceResultWithoutBaseEntity<CategoryRollup>> Add(CategoryRollupViewModelV2 newItem)
{
    ...
    foreach(var categoryChildId in newItem.CategoryChildID)
    {
       var categoryRollupEntity = new CategoryRollup
       {
           CategoryParentID = newItem.CategoryParentID,
           CategoryChildID = categoryChildId
       }
       _context.CategoryRollups.Add(categoryRollupEntity);
    }
    await _context.SaveChangesAsync();
    ...
}
Sign up to request clarification or add additional context in comments.

8 Comments

EF core does not support List<int> i think. i get this error "because it is of type 'List<int>' which is not a supported primitive type or a valid entity type."
I didn't change the entity but the view model. Have you tested the code?
It is having an error in the helper code on the line " CategoryChildID = source.CategoryChildID," it says cannot implicitly convert type int to list. What shoud i changed it to? thanks. helper code is included in the question
Comment the helper method because it is not necessary anymore and we will not use it.
How about this line sir " var item = newItem.MapToEntity(new CategoryRollup());" item is included in your code. MapEntity is in helper
|
0

You will need to understand how EF ORM works.

Will redesign your module a little.

Lets say you have

public class CategoryRollup
{
    [Key]
    public int ID { get; set; }

    // Now i assume that CategoryChildID refer to a list of CategoryRollup as children
    // then just make it so. you may have to config this in moduleBuilder 
    public List<CategoryRollup> CategoryChildren { get; set; }

    /// and this is the parent
    public CategoryRollup CategoryParent { get; set; }
}

Now your model could look like this, its upp to you though

public class CategoryRollupViewModel
{

    public CategoryRollupViewModel(CategoryRollup item){
      ID = item.ID;
      CategoryChildID = item.CategoryChildren.Select(x=> x.ID).ToList();
      CategoryParentID = item.CategoryParent?.ID; 
    }
    [Key]
    public int ID { get; set; }

    public List<int> CategoryChildID { get; set; }

    public int CategoryParentID { get; set; }

}

Did i understand you right ?

1 Comment

CategoryRollup is the model and table sir that includes The Child and Parent ID. I will test this sir thank you.

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.