Why does this IQueryable issue give me an error? - c#

I cannot figure out why code below is not working. It is causing a build error:
IQueryable<InventoryTbl>' does not contain a definition for 'FirstOrDefault' and the best extension method overload 'Queryable.FirstOrDefault<AdminModel>(IQueryable<Admin>)
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RuciyanaSweets.Models;
[HttpGet]
public ActionResult AdminAddorEdit(int id = 0)
{
if (id==0)
return View(new Model());
else
{
using (Entities db = new Entities ())
{
return View(db.TableName.Where(x => x.FieldID == id).FirstOrDefault< Model >());
}
}
}

The definition TSource FirstOrDefault<TSource>(this IQueryable<TSource> source) is part of System.Linq namespace. Check that your project referencing to System.Core and you have using System.Linq; and using System.Data.Entity;.
Be sure you installed the EntityFramework package.
Queryable.FirstOrDefault Method

Related

Type or namespace name DBModel could not be found

Trying to follow an asp.net tutorial. How how to import DBModel() class or what am I doing wrong?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication2.Models;
namespace WebApplication2.Controllers
{
public class CollegeController : Controller
{
// GET: College
public ActionResult Index()
{
return View();
}
public ActionResult GetData()
{
using (DBModel db = new DBModel())
{
}
}
}
}
If you are using Visual Studio or JetBrains Rider:
You need to add the directive to the namespace or assembly reference.
Try putting the cursor above the DBModel word and use key combination: ALT + Enter.
You should see something similar to the image below:
Most of the time you'll only see one suggestion. Click on it.

AddAsync() method is missing

I am using Entity Framework Core. I want to create an async method which will create new user in my database, i have included all the libraries I need, but some methods that are supposed to work with database are missing, I have almost every async method but i am missing AddAsync and RemoveAsync. When I type AddAsync manually I get the following error message: "Error CS1061 'DbSet' does not contain a definition for 'AddAsync' and no accessible extension method 'AddAsync' accepting a first argument of type 'DbSet' could be found (are you missing a using directive or an assembly reference?)"
The class where the method is created has following code and libraries included:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Data;
using Microsoft.EntityFrameworkCore;
namespace ClassLibrary
{
public class Class1 : Interface1
{
public async Task AddKorisnik(Korisnici k)
{
using (ExtentEntities context = new ExtentEntities())
{
context.Korisnici.AddAsync();
await context.SaveChangesAsync();
}
}
}
}
The class where the DbContext is used is following:
namespace ClassLibrary
{
using System;
using System.Linq;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class ExtentEntities : DbContext
{
public ExtentEntities()
: base("name=ExtentEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Korisnici> Korisnici { get; set; }
}
}
There is nothing asynchronous with adding an object to an in-memory DbSet<T>. You should use the synchronous Add method to do this.
SaveChangesAsync() is the method that actually connects asynchronously to the underlying database.

ApplicationContext.Current is coming null

I found same question in stack over flow as here and tried same think but still I am receiving ApplicationContext.Current as null.
I am making the web service where I need to pull out a couple of pieces of data from an Umbraco database. I don't need any of the Umbraco views or any of that stuff. I'm new to the Umbraco Core libraries.
What I did was get a below reference to my new web service project
umbraco.dll
Umbraco.Core.dll
umbraco.DataLayer.dll
umbraco.editorControls.dll
umbraco.MacroEngines.dll
umbraco.providers.dll
Umbraco.Web.UI.dll
umbraco.XmlSerializers.dll
UmbracoExamine.dll
And the below class code where I am receiving ApplicationContext.Current as null
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Web.Mvc;
using umbraco.MacroEngines;
using Umbraco.Web;
using Umbraco.Web.WebApi;
using umbraco.NodeFactory;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using umbraco.cms.businesslogic.media;
using umbraco.BusinessLogic;
using System.Configuration;
using log4net;
using System.Reflection;
using Umbraco.Core.Logging;
using System.Web.Optimization;
using System.Web.Http;
using System.Data.SqlClient;
using System.Data;
using System.Web.Script.Serialization;
using System.Collections;
using System.Collections.Specialized;
using System.Web.UI.WebControls;
using Stripe;
using System.Web.UI;
using System.Text.RegularExpressions;
using System.Web.Configuration;
using System.Web.Security;
using Newtonsoft.Json;
using System.Net.Mail;
using System.Text;
using System.Net;
using System.IO;
public class RegisterUserController : UmbracoApiController
{
public static string UmbracoConnectionString = ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString;
public static IMemberService memberService = ApplicationContext.Current.Services.MemberService;// Here I am getting ApplicationContext.Current as null
}
But I am calling this class I am getting ApplicationContext.Current as null.
So because of that I have Many methods in this class which works on memberService and due to this null reference they are not working. Even other methods which are not using member service are not being called.
For me, the code below seems to work.
public class TestApiController : UmbracoApiController
{
private static string _umbracoConnectionString = ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString;
private static IMemberService _memberService = global::Umbraco.Core.ApplicationContext.Current.Services.MemberService;
public int GetTest()
{
var memberCount = _memberService.Count();
return memberCount;
}
}
If the above code isn't working for you e.g. with AJAX, try the below code:
public class TestApiController : UmbracoApiController
{
public int GetTest()
{
var ms = Services.MemberService;
return ms.Count();
}
}

Entity Framework Windows App

I have EF application with following code.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WinFormswithEFSample
{
public class ObservableListSource<T> : ObservableCollection<T>, IListSource
where T : class
{
private IBindingList _bindingList;
bool IListSource.ContainsListCollection { get { return false; } }
IList IListSource.GetList()
{
return _bindingList ?? (_bindingList = this.ToBindingList());
}
}
}
But when build the project attached error comming. What is the error in this code?
Try add namespace System.Data.Entity which resides in EntityFramework.dll library (this assembly is necessery).

return all IQueryable with Entity framework

I just switched from Linq to entities framework, and I'm having problems with methods that returns "all rows". I get: "The type 'System.Data.Objects.DataClasses.EntityObject' is defined in an assembly that is not referenced" error in my "service layer" that calls the data-layer.
I get an error on:
BookingObjectRepository _repository = new BookingObjectRepository();
public IQueryable<BookingObject> GetBookingObjects()
{
return _repository.GetBookingObjects();
}
and in the "data-layer" I have:
BookingsystemEntities _entities = new BookingsystemEntities();
public IQueryable<BookingObject> GetBookingObjects()
{
return from bo in _entities.BookingObjectSet
select bo;
}
UPDATE: Filter items, they are "physically" in Filters-folder but namespace is same as the emdx file uses.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BookingSystem.Data.Models
{
public static class BookingObjectFilters
{public static IQueryable<BookingObject> ByBookingObjectID(this IQueryable<BookingObject> qry, int bookingobjectID)
{
return from bo in qry
where bo.BookingObjectID == bookingobjectID
select bo;
}
Your system must have .NET 3.5 SP 1 or better installed, and your project must reference the System.Data.Entity assembly (look at the references node in Solution Explorer).
Do you have
using System.Data;
using System.Data.Objects.DataClasses;
in your usings?
and
public IQueryable GetBookingObjects() { return _repository.GetBookingObjects(); }
should probably be
public IQueryable<BookingObject> GetBookingObjects() { return _repository.GetBookingObjects(); }
Hope that helps,
Dan

Categories