1
\$\begingroup\$

I'm working on my pet project with MVC 5 and EF, and everytime I'm adding a parent with it children (I have the children ID's) I have to go to the database, because if I just create a new child with this ID it just inserts a child object. Is there anyway to avoid this?

This is part of my code: My classes:

public class FeatureType
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual List<ItemType> ItemTypes { set; get; }
}

public class ItemType
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual List<FeatureType> FeatureTypes { get; set; }
}

What I have to do to insert FeatureTypes, for a Array of ItemTypesID (of my view Model) I go to the database and populate a List of ItemTypes.

 private IEnumerable<ItemType> GetItemTypes()
 {
     return ItemTypes.Select(id => unitOfWork.ItemTypeRepository.GetById(id));
 }

Then I just assign this list to my Model property and insert. Can I avoid this trip to the database?

If you need anything else from my code ask for it and I can post it or you can check it all at https://github.com/tellez12/Classifieds/

UPDATE:

This is my ViewModel where I get the itemTypes and call my repository to do the insert :

public class FeatureTypeViewModel
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public bool Required { get; set; }

        public string RequiredText { get; set; }

        public int ControlType { get; set; }

        public int Order { get; set; }

        public int SectionId { get; set; }

        public int[] ItemTypes { set; get; }

        public SelectList SectionSelect { get; set; }

        public SelectList ControllerTypeSelect { get; set; }

        public SelectList ItemTypeSelect { get; set; }

        private IUnitOfWork unitOfWork;

        public FeatureTypeViewModel()
        {
        }

        public FeatureTypeViewModel(FeatureType ft,IUnitOfWork myUnitOfWork)
        {
            SetRepositories( myUnitOfWork);

            Id = ft.Id;
            Name = ft.Name;
            Required = ft.Required;
            RequiredText = ft.RequiredText;
            ControlType = (int)ft.ControlType;
            Order = ft.Order;
            SectionId = ft.Section.Id;
            ItemTypes = getItemTypesId(ft.ItemTypes).ToArray();
            FillSelectList();
        }

        public void SetRepositories(IUnitOfWork myUnitOfWork)
        {
            unitOfWork = myUnitOfWork;

        }

        public FeatureTypeViewModel(IUnitOfWork myUnitOfWork)
        {
            SetRepositories( myUnitOfWork);
            FillSelectList();
        }

        private void FillSelectList()
        {
            SectionSelect = new SelectList(unitOfWork.SectionRepository.Get().ToList(), "Id", "Name", SectionId);
            ItemTypeSelect = new SelectList(unitOfWork.ItemTypeRepository.Get().ToList(), "Id", "Name", ItemTypes);

            var typeEnumSelect = from ControlType s in Enum.GetValues(typeof(ControlType))
                                 select new { ID = (int)s, Name = s.ToString() };
            ControllerTypeSelect = new SelectList(typeEnumSelect, "ID", "Name", ControlType);
        }

        private IEnumerable<int> getItemTypesId(IEnumerable<ItemType> list)
        {
            return list.Select(item => item.Id);
        }

        public FeatureType ToModel()
        {
            var ft = new FeatureType
                         {
                             Name = Name,
                             ControlType = (ControlType)ControlType,
                             Order = Order,
                             Required = Required,
                             RequiredText = RequiredText,
                             ItemTypes = GetItemTypes().ToList(),
                             Section = GetSection(),
                         };

            return ft;
        }

        private Section GetSection()
        {
            var section = unitOfWork.SectionRepository.GetById(SectionId);
            return section;
        }

        private IEnumerable<ItemType> GetItemTypes()
        {
            return ItemTypes.Select(id => unitOfWork.ItemTypeRepository.GetById(id));
        }
    }
\$\endgroup\$
  • \$\begingroup\$ I like my navigation properties to be ICollection - List<T> is an implementation of that interface. \$\endgroup\$ – Mathieu Guindon Dec 17 '13 at 18:48
1
\$\begingroup\$

Since FeatureType - ItemTupe is a many-to-many association you can only handle it as an independent association, because there is no junction class in the class model that exposes the primitive foreign key fields.

So, no, you can't prevent database roundtrips to fetch the objects that you need to populate the many-to-many association. A (micro) optimization could be to cache the objects in case you're going to create multiple FeatureTypes.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.