locked
asp.net web api 2 can not query with lambda RRS feed

  • Question

  • User-1104215994 posted

    Hello,

    I am trying to query the database with a username from the request. But I am getting this error; The type 'Func<,>' is defined in an assembly that is not referenced. You must add a reference to assembly '<g class="gr_ gr_124 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="124" data-gr-id="124">netstandard</g>. How can I fix this?

    Here is a portion of my controller:

    [RoutePrefix("api/v3/token")]
        public class CompaniesController : ApiController
        {
            private EPINMiddleWareAPIContext context;
    
            public CompaniesController(EPINMiddleWareAPIContext context)
            {
                this.context = context;
            }
           
            
            // POST: api/Companies
            [ResponseType(typeof(Company))]
            [HttpPost, Route("company")]
            public async Task<IHttpActionResult> Company(Company company)
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }
    
                
                context.Companies.Add(company);
                await context.SaveChangesAsync();
    
               var c = context.Companies.FirstOrDefault<Company>(p => p.userName == company.userName);
    
                IHttpActionResult response;
                HttpResponseMessage responseMsg = new HttpResponseMessage();
                bool isUsernamePasswordValid = false;
    ...

    Here is my model:

    public class Company
        {
            public int Id { get; set; }
            [Required]
            public string userName { get; set; }
            [Required]
            public string hash { get; set; }
            [Required]
            public string salt { get; set; }
            [Required]
            public int customerID { get; set; }
        }

    Best Regards.

    Tuesday, March 5, 2019 6:51 PM

Answers

  • User475983607 posted

    cenk1536

    asp.net web api 2

    The error is very clear.  It seems you built a netstandard library.  Please review your work.

    https://docs.microsoft.com/en-us/dotnet/standard/net-standard

    Also the code looks like ASP.NET Core constructor injection. Are you sure?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 5, 2019 7:47 PM
  • User1120430333 posted

    Why don't you just use the Where clause? The examples are using EF core. 

    public DtoProject GetProjectById(int id)
            {
                var dto = new DtoProject();
    
                using (var context = new ProjectManagementContext(_options))
                {
                    var project = (context.Projects.Where(a => a.ProjectId == id)).SingleOrDefault();
    
                    if (project == null) return dto;
                    dto.ProjectId = project.ProjectId;
                    dto.ClientName = project.ClientName;
                    dto.ProjectName = project.ProjectName;
                    dto.Technology = project.Technology;
                    dto.ProjectType = project.ProjectType;
                    dto.UserId = project.UserId;
                    dto.StartDate = project.StartDate;
                    dto.EndDate = project.EndDate;
                    dto.Cost = project.Cost;
                }
    
                return dto;
            }
    public List<DtoProject> GetProjectsByUserId(string userid)
            {
                var dtos = new List<DtoProject>();
    
                using (var context = new ProjectManagementContext(_options))
                {
                    
                    dtos = (from a in context.Projects.Where(a => a.UserId.Contains(userid))
                        select new DtoProject
                        {
                            ProjectId = a.ProjectId,
                            ClientName = a.ClientName,
                            ProjectName = a.ProjectName,
                            Technology = a.Technology,
                            ProjectType = a.ProjectType,
                            UserId = a.UserId,
                            StartDate = a.StartDate,
                            EndDate = a.EndDate,
                            Cost = a.Cost
                        }).ToList();
                }
    
                return dtos;
            }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 5, 2019 8:52 PM

All replies

  • User475983607 posted

    Remove company...  it is redundant.

     var c = context.Companies.FirstOrDefault(p => p.userName == company.userName);

    You are missing a reference.  Is this an ASP.NET Core application?

    https://github.com/dotnet/standard/issues/542

    Tuesday, March 5, 2019 7:00 PM
  • User-1104215994 posted

    same error;

    Severity Code Description Project File Line Suppression State
    Error CS0012 The type 'Func<,>' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

    Tuesday, March 5, 2019 7:03 PM
  • User475983607 posted

    same error;

    Severity Code Description Project File Line Suppression State
    Error CS0012 The type 'Func<,>' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

    You are missing a reference.  If this is an ASP.NET Core application and a separate assembly, add the netstandard the project file.

    Tuesday, March 5, 2019 7:08 PM
  • User-1104215994 posted

    You are missing a reference.  Is this an ASP.NET Core application?

    asp.net web <g class="gr_ gr_26 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="26" data-gr-id="26">api</g> 2

    Tuesday, March 5, 2019 7:11 PM
  • User475983607 posted

    cenk1536

    asp.net web api 2

    The error is very clear.  It seems you built a netstandard library.  Please review your work.

    https://docs.microsoft.com/en-us/dotnet/standard/net-standard

    Also the code looks like ASP.NET Core constructor injection. Are you sure?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 5, 2019 7:47 PM
  • User1120430333 posted

    Why don't you just use the Where clause? The examples are using EF core. 

    public DtoProject GetProjectById(int id)
            {
                var dto = new DtoProject();
    
                using (var context = new ProjectManagementContext(_options))
                {
                    var project = (context.Projects.Where(a => a.ProjectId == id)).SingleOrDefault();
    
                    if (project == null) return dto;
                    dto.ProjectId = project.ProjectId;
                    dto.ClientName = project.ClientName;
                    dto.ProjectName = project.ProjectName;
                    dto.Technology = project.Technology;
                    dto.ProjectType = project.ProjectType;
                    dto.UserId = project.UserId;
                    dto.StartDate = project.StartDate;
                    dto.EndDate = project.EndDate;
                    dto.Cost = project.Cost;
                }
    
                return dto;
            }
    public List<DtoProject> GetProjectsByUserId(string userid)
            {
                var dtos = new List<DtoProject>();
    
                using (var context = new ProjectManagementContext(_options))
                {
                    
                    dtos = (from a in context.Projects.Where(a => a.UserId.Contains(userid))
                        select new DtoProject
                        {
                            ProjectId = a.ProjectId,
                            ClientName = a.ClientName,
                            ProjectName = a.ProjectName,
                            Technology = a.Technology,
                            ProjectType = a.ProjectType,
                            UserId = a.UserId,
                            StartDate = a.StartDate,
                            EndDate = a.EndDate,
                            Cost = a.Cost
                        }).ToList();
                }
    
                return dtos;
            }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 5, 2019 8:52 PM
  • User-1104215994 posted

    Why don't you just use the Where clause? The examples are using EF core. 

    I will try and let you know.

    Wednesday, March 6, 2019 5:10 AM
  • User-1104215994 posted

    Adding <Reference Include="<g class="gr_ gr_25 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="25" data-gr-id="25">netstandard</g>" /> to <g class="gr_ gr_26 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="26" data-gr-id="26">csproj</g> file solved the issue. And where clause also worked.

    Wednesday, March 6, 2019 5:13 PM