Base solution for your next web application
Open Closed

InsertOrUpdate method and 2 way-binding on Entity Framework #366


User avatar
0
mrvithan created

I am trying to use InsertOrUpdate method in IRepository. I can insert an entity but not for update. It throws an error saying the same entity key is exists. Do you have any example how to use it ?

And another question. How to create 2 way-binding in Entity Framework ? for example, A task has a assigned person. but from person class, how to automatically has "Tasks" that the person is on. Now i have manual call ITaskIRepository.where(p => p.PersonId == id) to get the task list. I know this is quite not related with ABP, but a help is very appreciated.

PS. if you would like me to split into 2 question, please lets me know.

Thx for help


3 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    For your second question, why don't you use navigation collection property in the Person entity (like public virtual ICollection<Task> Tasks)?

    For the first one, can you create an issue on Github repository? Also, please copy your code so we may understand it better.

    Thanks.

  • User Avatar
    0
    mrvithan created

    For the first issue: here is the shot code snap:

    I have a IInputDto class said :

    public class TheaterUpdate : IInputDto { public int Id;

    [Required] public string name; }

    Then in a AppService said :

    public class TheaterAppService: ITheaterAppService { public void AddOrUpdate(TheaterUpdate entity) { var obj = new Theater() obj.Id = entity.Id; obj.Name = entity.Name; _repo.InsertOrUpdate(obj);

    } }

    then i will get the Exception said there is already has the same entity key exists. And suggestion ??

    For the second issue, i will try in couple days and will feedback asap.

  • User Avatar
    0
    hikalkan created
    Support Team

    Replace your this code:

    var obj = new Theater() obj.Id = entity.Id; obj.Name = entity.Name; _repo.InsertOrUpdate(obj);

    with this one:

    var obj = _repo.Get(entity.Id); obj.Name = entity.Name;

    No need to call update. This is a better practice then creating a new Theater. Always get entity from repository, update needed fields. You could also use AutoMapper to map values.

    For InsertOrUpdate, can you create an issue on Github.

    Thanks.