Base solution for your next web application
Open Closed

AppService issue #1522


User avatar
0
vitaly created

Hello, could you help to analyze the issue?

I have the following Task in AppService:

public async Task<StoreDto> CreateOrUpdateStore(CreateOrUpdateStoreInput input)
        {
            if (input.Store.Id.HasValue)
            {
                var store = await _storeRepository.GetAsync(input.Store.Id.Value);
                input.Store.MapTo(store);
                await _storeRepository.UpdateAsync(store);
                return store.MapTo<StoreDto>();
            }
            else
            {
                var store = input.Store.MapTo<Store>();
                await _storeRepository.InsertAsync(store);
                return store.MapTo<StoreDto>();
            }
        }

StoreDto:

[AutoMap(typeof(Store))]
    public class StoreDto
    {
        public int? Id { get; set; }
        ...
        
        //Schedules
        public Collection<StoreScheduleDto> StoreSchedules { get; set; }
    }

StoreScheduleDto:

[AutoMap(typeof(StoreSchedule))]
    public class StoreScheduleDto
    {
        public int? Id { get; set; }
        public int DayOfWeek { get; set; }                  // 0-Every day 1-Mo, 2-Tu, 3-We, 4-Th, 5-Fr, 6-Sa, 7-Su
        public bool Active { get; set; }
        public string StartTime { get; set; }
        public string FinishTime { get; set; }
    }

When I create new Store with collection of StoreSchedules it's working good. But when I try to update existing item, sending to AppService input:

"input": {
        "store": {
            "id": 7,
            "storeGroupId": 1,
            "name": "asdfdf",
            "storeSchedules": [
                {
                    "id": 25,
                    "dayOfWeek": 0,
                    "active": false,
                    "startTime": "09:00",
                    "finishTime": "20:00"
                },
                {
                    "id": 26,
                    "dayOfWeek": 1,
                    "active": true,
                    "startTime": "03:30",
                    "finishTime": "23:30"
                },
               ...

it throw the following error:

System.InvalidOperationException: The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted. at System.Data.Entity.Core.Objects.ObjectContext.PrepareToSaveChanges(SaveOptions options) at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesInternalAsync>d__31.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Abp.EntityFramework.AbpDbContext.<SaveChangesAsync>d__34.MoveNext() in D:\Halil\GitHub\aspnetboilerplate\src\Abp.EntityFramework\EntityFramework\AbpDbContext.cs:line 196 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Abp.EntityFramework.Uow.EfUnitOfWork.<SaveChangesInDbContextAsync>d__22.MoveNext() in D:\Halil\GitHub\aspnetboilerplate\src\Abp.EntityFramework\EntityFramework\Uow\EfUnitOfWork.cs:line 198 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at...


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

    Hi,

    Entity Framework does not support such an update opeation. We manually update entities on such situations (detecting added/deleted/updated entities and using appropriate repository methods to insert/delete/update them). This question is asked in the forum before several times. There are some 3rd party projects solve this problem (likje GraphDiff). For example, see #1251@d6eef456-9b9a-4478-92ff-fed8877fd287