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.
Related
I added a class library to an API project to get the data from database. In the class library "DataManager" I created a folder named "Entity" and added ADO entity data models for for sql and oracle db. When I was creating the ADO for oracle I had to degrade entity framework version from 6 to 5.
Models.Context.cs
namespace DataManger.Entity
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class myEntities : DbContext
{
public myEntities()
: base("name=myEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<Application> Applications { get; set; }
Now when I try to get any of the models in "Entity".
using DataManager.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
namespace DataManager.Services
{
public class ApplicationDataService : IApplicationDataService
{
I get the following error message in the first line when using DataManager.Entity
Severity Code Description Project File Line Suppression State
Error CS0234 The type or namespace name 'Entity' does not exist in the namespace 'DataManager' (are you missing an assembly reference?) DataManger ......
I am using Xamarin.Forms MVVM and sqlLite-net to make a Shared Mobile App.
I am getting an error bc 'AddRange' doesn't exist in ObservableCollection or I am using that function wrong.
All I want to do is to fill my ObservableCollection list. do I have to manually Add using loop? its weird ObservableCollection has a 'Add' method but no way to fill
Error: 'ObservableCollection' does not contain a definition for 'AddRange' and no accessible extension method 'AddRange' accepting a first argument of type 'ObservableCollection' could be found (are you missing a using directive or an assembly reference?)
using following refs:
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using TestApp01_MVVM_Basic.Models;
using Xamarin.CommunityToolkit.ObjectModel;
using Xamarin.Forms;
using System.Collections.Specialized;
using TestApp01_MVVM_Basic.Services;
using System.Collections.Generic;
using System.Linq;
Below I am creating my ObservableCollection. I am getting my error on line: AddRange()
public ObservableCollection<ProductModel> ProductsList { get; set; }
public ProductViewModel()
{
ProductsList = new ObservableCollection<ProductModel>();
}
public async Task MyDisplayList()
{
var getData = ProductService.DisplayProduct();
ProductsList.AddRange(getData);
}
In Servies Class, I have following DisplayProduct() method. that returns a list from database
public static async Task<IEnumerable<ProductModel>> DisplayProduct()
{
await Init();
var GetProduct = await db.Table<ProductModel>().ToListAsync();
return GetProduct;
}
What you are looking for is implemented on Xamarin community toolkit package: ObservableRangeCollection which sub-class System.Collections.ObjectModel.ObservableCollection<T>, you can use ObservableRangeCollection<ProductModel> instead of ObservableCollection<ProductModel> since the latter does not implement such method.
public ObservableRangeCollection<ProductModel> ProductsList { get; set; }
public ProductViewModel()
{
ProductsList = new ObservableRangeCollection<ProductModel>();
}
public async Task MyDisplayList()
{
var getData = await ProductService.DisplayProduct();
ProductsList.AddRange(getData);
}
public static async Task<IEnumerable<ProductModel>> DisplayProduct()
{
await Init();
var GetProduct = await db.Table<ProductModel>().ToListAsync();
return await Task.FromResult(GetProduct);
}
Got a Noob question. I am trying to write a C# MVC Application to connect to an MS SQL DB using the Entity Framework. But I am getting the following error.
Severity Code Description Project File Line Suppression State
Error CS1061 'DatabaseFacade' does not contain a definition for 'SqlQuery' and no extension method 'SqlQuery' accepting a first argument of type 'DatabaseFacade' could be found (are you missing a using directive or an assembly reference?) HAPI.DNX 4.5.1, HAPI.DNX Core 5.0
My Code is as follows
DB Context Class which flags no issues.
using Microsoft.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Something.Models
{
public class RulesDbContext: DbContext
{
public DbSet<Rules> Rules { get; set; }
}
}
Calling Class with errors on the Sql Statements highlighted in bold
using System.Collections.Generic;
using System.Linq;
using Microsoft.Data.Entity;
namespace Something.Models
{
public class ReadRules
{
private RulesDbContext db = new RulesDbContext();
private void Rules()
{
List<Rules> rulesMeta = db.Database.**SqlQuery**<Rules>("GetRuleMetaData #deviceID",
new **SqlParameter**("deviceID", deviceID)).ToList();
}
}
}
A bit rusty here with regards to WCF Services.
I have a custom class named cSecurity.cs which does some custom functions.
I want to use this custom class inside my Service:
This is the App.svc.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace AppServices
{
public class App : IApp
{
public cSecurity _csec;
public string GetItems(int agentID, string agentName)
{
// Need to use some functions from the cSecurity class here???
return _csec.getItems();
}
}
}
This is the cSecurity.cs class:
using System.Collections;
using System.Configuration;
using System.Net;
namespace AppServices
{
public class cSecurity
{
// Some functions defined here....
public string getItems(){
return string.Empty;
}
}
}
But I am getting an error on the line:
public cSecurity _csec;
"The type or namespace name 'cSecurity' could not be found."
This seems pretty trivial but I seem to be lost here.
Appreciate any insight. Thanks.
For some reason, the solution for this was for me to close and restart VS. Not sure what caused the class for it to be not referenced by VS.
In my demo solution, I have a two projects DataAccess and WebInterface.
In DataAccess project, I have one EF for Users and a class file UserDataAccessService.cs
Code in UserDataAccessService.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data.EntityClient;
using System.Data.SqlClient;
using System.Data;
using DataAccess.User;
namespace DataAccess.User
{
public sealed class UserDataAccessService
{
private string efConnectionString;
private string portalConnectionString;
public string ConnectionString
{
get { return efConnectionString; }
}
public UserDataAccessService(string portalConnectionString)
{
this.portalConnectionString = portalConnectionString;
this.efConnectionString = GetConnectionString(portalConnectionString);
}
private string GetConnectionString(string portalConnectionString)
{
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = "System.Data.SqlClient";
entityBuilder.ProviderConnectionString = portalConnectionString;
entityBuilder.Metadata = "res://*/User.ProgramModel.csdl|res://*/User.UserModel.ssdl|res://*/User.UserModel.msl";
return entityBuilder.ToString();
}
public IEnumerable<User> GetUsers(int? userId, bool? status)
{
using (Users objectContext = new Users(efConnectionString))
{
return objectContext.GetUsers(null, null);
}
}
}
}
Now in WebInterface project, I simply have a ManageUsers.aspx page on which I am trying to fetch list of users by below code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using DataAccess.User;
namespace WebInterface
{
public partial class ManageUser : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UserDataAccessService usrObj = new UserDataAccessService(ConfigurationManager.ConnectionStrings["portalConn"].ToString());
List<User> userLst = new List<User>();
userLst = usrObj.GetUsers(1, null).ToList();
}
}
}
Problem:-
When I am building the solution then I am getting the below error:-
Error 1
The type 'System.Data.Objects.DataClasses.EntityObject' is defined in an assembly that is not referenced.
You must add a reference to assembly 'System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
D:\Practice\PTP\UI\WebInterface\User\ManageUser.aspx.cs
18 13
WebInterface
To fix it, I have included it System.Data.Entity.dll in reference folder of WebInterface project which has fixed the issue.
My Question:
If you look into my the code then you can that code related EF is all in DataAccess project which has all required dll reference in it required for classes related to EF.
and in WebInterface project I have included DataAccess.dll and in it .aspx page I am simply calling it function to the fetch data.
Then why it requires System.Data.Entity.dll in WebInterface project too?
Regards
Varun
I am only 99% sure cause you are using some old school EF :) and you didn't share User class but... I think it's the User class that is EntityFramework aware. Is it the case that User inherits from EntityObject?
You can avoid that by mapping data from entity classes to you UI classes. But that takes all the magic from using EF.