Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Not found table for clr type" when make OData POST request #53

Closed
Makarov628 opened this issue Feb 9, 2021 · 2 comments
Closed

"Not found table for clr type" when make OData POST request #53

Makarov628 opened this issue Feb 9, 2021 · 2 comments
Labels

Comments

@Makarov628
Copy link
Contributor

Makarov628 commented Feb 9, 2021

Description

I am having trouble to insert an object using a POST request. DictAreas entity are associated with a one-to-many DictCities entity. When I submit a request, it sends me a response with this exception and a 500 status code, but when I check the DB, I see that the value for DictAreas has been added. Is it possible to somehow disable the hierarchical data insertion mode or solve this problem differently?

I am using models created by Linq2Db with T4 templates (MS SQL Server). We are also trying to make one controller for all entities, GET and PATCH requests works fine, but there are problems with this exception.

Exception

System.InvalidOperationException: Not found table for clr type ExampleProject.DB.DictCity
   at OdataToEntity.Linq2Db.OeLinq2DbDataContext.UpdateIdentities(OeLinq2DbTable table, Int32 lastIndex)
   at OdataToEntity.Linq2Db.OeLinq2DbDataContext.SaveChanges(DataConnection dataConnection)
   at OdataToEntity.Linq2Db.OeLinq2DbDataAdapter`1.SaveChangesAsync(Object dataContext, CancellationToken cancellationToken)
   at OdataToEntity.AspNetCore.OeBatchFilterAttributeAttribute.OnActionExecuted(ActionExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
--- End of stack trace from previous location ---

Code

Models

using System;
using System.Collections.Generic;

using LinqToDB;
using LinqToDB.Data;
using LinqToDB.Mapping;

using OdataToEntity.Linq2Db;


namespace ExampleProject.DB
{
    public partial class ExampleDB : DataConnection, IOeLinq2DbDataContext
    {
        OeLinq2DbDataContext IOeLinq2DbDataContext.DataContext
        {
            get;
            set;
        }
        public ITable<DictArea> DictAreas { get { return this.GetTable<DictArea>(); } }
        public ITable<DictCity> DictCities { get { return this.GetTable<DictCity>(); } }
        
    }

    [Table(Schema = "dbo", Name = "DictArea")]
    public partial class DictArea
    {
        [PrimaryKey, Identity] public int AreaID { get; set; } // int
        [Column, NotNull] public string Name { get; set; } // nvarchar(50)
        [Column, Nullable] public string Code { get; set; } // nvarchar(10)
 
        [Association(ThisKey = "AreaID", OtherKey = "AreaID", CanBeNull = true, Relationship = LinqToDB.Mapping.Relationship.OneToMany, IsBackReference = true)]
        public IEnumerable<DictCity> DictCities { get; set; }

    }


    [Table(Schema = "dbo", Name = "DictCities")]
    public partial class DictCity
    {
        [PrimaryKey, Identity] public int CitiesID { get; set; } // int
        [Column, NotNull] public string Name { get; set; } // nvarchar(50)
        [Column, NotNull] public int AreaID { get; set; } // int
        [Column, Nullable] public bool? IsVillage { get; set; } // bit
        [Column, NotNull] public bool IsActive { get; set; } // bit
        [Column, Nullable] public string Code { get; set; } // nvarchar(10)
        [Column, NotNull] public bool IsDefault { get; set; } // bit
        [Column, Nullable] public int? LocationID { get; set; } // int

    
        [Association(ThisKey = "AreaID", OtherKey = "AreaID", CanBeNull = false, Relationship = LinqToDB.Mapping.Relationship.ManyToOne, KeyName = "FK_DictCities_DictArea", BackReferenceName = "DictCities")]
        public DictArea Area { get; set; }

    }
}

Controller

using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.OData.Edm;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;

using OdataToEntity.Query;
using OdataToEntity.AspNetCore;
using ExampleProject.DB;

namespace ExampleProject.Controllers
{
    [Route("api/v1/[controller]")]
    public sealed class DictAreasController
    {
        private readonly IHttpContextAccessor _httpContextAccessor;

        public DictAreasController(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }

        [HttpGet]
        public async Task Get()
        {
            var edmModel = (IEdmModel)_httpContextAccessor.HttpContext.RequestServices.GetService(typeof(IEdmModel));
            OeModelBoundProvider modelBoundProvider = OeAspHelper.CreateModelBoundProvider(edmModel, 50, false);
            await OeAspQueryParser.Get(_httpContextAccessor.HttpContext, modelBoundProvider).ConfigureAwait(false);
        }

        [HttpPost]
        public void Post(OeDataContext dataContext, DictArea obj)
        {
            dataContext.Update(obj);
        }
        
        [HttpPatch]
        public void Patch(OeDataContext dataContext, IDictionary<String, Object> obj)
        {
            dataContext.Update(obj);
        }
    }
}
@voronov-maxim
Copy link
Owner

Hi @Makarov628

Send me minimal solution for reproduce this issue and sql script for create schema

@Makarov628
Copy link
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants