C# MVC7 DNX5 MSSQL - c#

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();
}
}
}

Related

Asp.net: The type or namespace name 'Entity' does not exist in the namespace 'DataManager' (are you missing an assembly reference?)

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 ......

SolrNet in C# example

I am trying to write a simple c# program to just connect to my Solr instance. I have downloaded the SolrNet library and made reference to SolrNet.dll in my project. I am following some of the examples online but have been getting the error "ServiceLocationProvider must be set" when the last line of my code is executed. Following is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using CommonServiceLocator;
using SolrNet;
using SolrNet.Attributes;
using SolrNet.Impl;
using SolrNet.Impl.DocumentPropertyVisitors;
using SolrNet.Impl.FacetQuerySerializers;
using SolrNet.Impl.FieldParsers;
using SolrNet.Impl.FieldSerializers;
using SolrNet.Impl.QuerySerializers;
using SolrNet.Impl.ResponseParsers;
using SolrNet.Mapping;
using SolrNet.Mapping.Validation;
using SolrNet.Mapping.Validation.Rules;
using SolrNet.Schema;
using SolrNet.Utils;
namespace WebApplication1
{
public class Post
{
[SolrField("id")]
public string id { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
var connection = new SolrConnection("http://localhost:8983/solr/test");
Startup.Init<Post>(connection.ServerURL);
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Post>>();
}
}
}
Can someone tell me what am I missing?
Thank You

How to resolve error On List collection it's can't accept or execut ToList() method?

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using MahadevBhaktDB;
using MahadevBhaktModal;
namespace MahadevBhakt_Dewasi_.Controllers
{
public class AdminController : Controller
{
public ActionResult Index()
{
return View();
}
public List<Tab_Country> GetCountryList()
{
DSPDatabaseEntities db = new DSPDatabaseEntities();
List<Tab Country> tab_Countries = db.tab_Countries.Tolist();
return tab_Countries;
}
}
}
'object' does not contain a definition for 'ToList' and no accessible extension method 'ToList' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) .
This kind of error is come and but i try to import some packages but there is not available so how to pick that kinds of package in my project.

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.

Basics of Entity Framework

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.

Categories