0

I had a problem trying to add object from dto.

I have a DTO like this

  public class CustomerCourseSessionDto : IDto
  {
    public int Id { get; set; }
   
    public int? ConnectorId { get; set; } 
    public List<SomeObject>? SomeObject { get; set; }
    public List<OtherObject>? OtherObject { get; set; }
  }

and I'm trying to insert this DTO sent from the UI. I'm using a loop(foreach) for this, but it didn't feel right to me.

Maybe entityframework suggests a method for this, idk...

Other objects are like this

 public class SomeObject: IEntity
  {
    public int Id { get; set; }
    public int? ConnectorId { get; set; } 
  }
 public class OtherObject: IEntity
  {
    public int Id { get; set; }
    public int? ConnectorId { get; set; } 
  }

Thanks for the suggestions.

Here is where I use the loop:

    [TransactionScopeAspect]
        public IResult AddWithDto(CustomerCourseSessionDto courseSessionDto)
        {
    
          foreach (var item in courseSessionDto.Participants)
          {
            _someObjectService.Add(item);
          }
    
          foreach (var item in courseSessionDto.Lessons)
          {
            _otherObjectService.Add(item);
          }
    
          _customerCourseSessionDal.Add(new CustomerCourseSession
          {
            Id = courseSessionDto.Id,
            CustomerCourseId = courseSessionDto.CustomerCourseId,
          
          });
          return SuccessResult with message ;
        }
7
  • Can you please show the code that you're actually using? The one you mention in "I'm using a loop(foreach) for this". Commented Oct 25, 2022 at 14:11
  • Question edit with loop code Commented Oct 25, 2022 at 14:17
  • Remove all this code and that TransactionScopeAspect. Just someContext.CourseSession.Add(sessionAndAll); is enough. That will add all related objects in the Added state. A DbContext is already a Unit-of-Work, so when SaveChanges is called all changes will be persisted in a single database transaction Commented Oct 25, 2022 at 14:18
  • BTW there's no EF Core in the question. No DbContext. TransactionScopeAspect isn't an EF Core attribute nor would it matter. All pending changes are saved with SaveChanges. There are multiple "services" which means that either EF isn't used at all, or it's used incorrectly. At best those "services" are just wrappers over an injected DbContext Commented Oct 25, 2022 at 14:19
  • This code is working well but is there any other way to do it without loop? Commented Oct 25, 2022 at 14:20

1 Answer 1

1

You should design your entities to have a link to each table using foreign keys.

    public class CustomerCourseSession
    {
        public int Id { get; set; } // Primary Key
        public string Name { get; set; }
        public virtual ICollection<CourseParticipant> Participants { get; set; }
        public virtual ICollection<CourseLesson> Lessons { get; set; }
    }

    public class CourseParticipant
    {
        public int Id { get; set; }
        public int CustomerCourseSessionId { get; set; } // Foreign Key from CustomerCourseSession
        // other fields here
        
        public virtual CustomerCourseSession CustomerCourseSession { get; set;}
    }
    
    public class CourseLesson
    {
        public int Id { get; set; }
        public int CustomerCourseSessionId { get; set; } // Foreign Key from CustomerCourseSession
        // other fields here
        
        public virtual CustomerCourseSession CustomerCourseSession { get; set;}
        
    }

Then you can just call:

var entity = MapDtoToEntity(courseSessionDto);

_dbContext.CustomerCourseSessions.Add(entity);

_dbContext.SaveChanges();
Sign up to request clarification or add additional context in comments.

3 Comments

I changed my design like you say but it didn't work :S
What do you mean it didn't work? can you post some of you code?
Of course I can , which part exactly ?

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.