LinqToSql - Explicitly constructing object type '{0}' in query is not allowed

I am facing a very strange problem with two simple webapi methods:

I have many methods that are used to fetch masterdata from db. They are all similar, and they all read tables with the same structures.

I really don't know why, for example, this method works:

works

[HttpGet]
//[Authorize]
[Route("offertsource/get")]
public IHttpActionResult api_MasterData_OffertSource_GET()
{
    var res = new List<mdOffertSource>();
    try
    {
        res = (from c in db.mdOffertSources.OrderBy(x => x.OffertSource)
               select new mdOffertSource() { idOffertSource = c.idOffertSource, Code = c.Code, OffertSource = c.OffertSource }).ToList();

        return Ok(res);
    }
    catch (Exception e)
    {
        return BadRequest(e.Message);
    }
}

      

and this doesn't work:

NOT working code

[HttpGet]
//[Authorize]
[Route("offertstatus/get")]
public IHttpActionResult api_MasterData_OffertStatus_GET()
{
     var res = new List<mdOffertStatus>();
     try
     {
         res = (from c in db.mdOffertStatus.OrderBy(x => x.intOrder).ThenBy(x => x.OffertStatus)
               select new mdOffertStatus() { idOffertStatus = c.idOffertStatus, Code = c.Code, OffertStatus = c.OffertStatus }).ToList();

        return Ok(res);
    }
    catch (Exception e)
    {
        return BadRequest(e.Message);
    }
}

      

I have many methods similar to the first one and only the second one doesn't work.

If I test it, I get this error:

Explicit construction of the object type "XionDB.mdOffertStatus" in the request is not allowed.

I already tried to remove the mdOffertStatus table from the dbml file and add it again, but I am facing the same problem ...

It is very strange.

Thanks to the support

+3


source to share


2 answers


I solved my problem by deleting the dbml file and adding it again ...

Before I tried to delete and add the mdOffertStatus table again but nothing changed, resetting the dbml and adding it again fixed the problem.



Maybe there was some inconvenience in the solution ... Thanks to the support

0


source


Your OfferSources table isn't empty, is it? This will prevent Select from being called and avoid the error thrown in the second method.



(Would leave this as a comment, but doesn't score enough.)

+1


source







All Articles