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.
Related
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.
I am new with C#. I am using Visual Studio 13, my problem is that it's throwing me this error:
The type or namespace name 'SQLite' does not exist in the namespace 'System.Data' (are you missing an assembly reference?)
Yes, I added references from here https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
and I followed the tutorial from here :
https://www.youtube.com/watch?v=N0hL5sGkUSA
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;
namespace PercobaanDulu
{
public partial class Form1 : Form
{
string connString;
public Form1()
{
InitializeComponent();
connString = #" Data Source = C:\Users\Febry Fairuz\Desktop\dbDemoLagi.db; Version = 3";
}
private void btnConnect_Click(object sender, EventArgs e)
{
using (SQLiteConnection con = new SQLiteConnection(connString)) {
try {
con.Open();
if(con.State == ConnectionState.Open){
MessageBox.Show("Koneksi berhasil dengan SQLite");
}
}catch(Exception ex){
MessageBox.Show(ex.Message);
}
}
}
}
}
Can you help me ?
I think the problem is that you are referencing incorrect version of System.Data.Sqlite.dll which is not compatible with version of .NET which you are using. You should check and install the compatible version of the dll to get rid of your problem.
If you are using the .Net 4.5 then you can try the NuGet package System.Data.SQLite.
A direct link for the package is here
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.
I recently inherited another vendor's project and I am trying to turn it into a usable Visual Studio 2010 solution.
The error I'm currently running into is:
Make sure that the class defined in this code file matches the
'inherits' attribute, and that it extends the correct base class (e.g.
Page or UserControl).
I have found this question posed several other times, but none of the solutions seem to work in this case. Below is the code in question:
~/layouts/Header.ascx.cs
using System;
using Client._Classes.Global;
using Client._Classes.Helpers;
using Client._Classes.Utilities;
namespace Layouts.layouts_Header
{
public partial class layouts_Header : BaseControl
{
private void Page_Load(object sender, EventArgs e)
{
}
protected void lnkSignIn_OnClick(object sender, EventArgs e)
{
Session["CurrentPageURL"] = Sitecore.Context.RawUrl;
Response.Redirect("/en/Community/Register.aspx");
}
protected void btnSearchSubmit_OnClick(object sender, EventArgs e)
{
string redirectURL = "/en/Search%20Results.aspx?cx=005917832522243245879:kpcudcaotoo&cof=FORID:11&ie=UTF-8&q=" + txtSearchQueryStr.Text;
Response.Redirect(redirectURL);
}
}
}
~/layouts/Header.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeFile="~/layouts/Header.ascx.cs" Inherits="Layouts.layouts_Header.layouts_Header" %>
BaseControl.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Globalization;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Web;
using Sitecore.SharedSource.Data.Comparers.ItemComparers;
using Sitecore.Data.Fields;
using Sitecore.SharedSource.Searcher;
using Sitecore.SharedSource.Searcher.Parameters;
using Sitecore.SharedSource.Searcher.Utilities;
using Sitecore.Collections;
using Client._Classes.Utilities;
using Client._Classes.Helpers;
using Client._Classes.Global;
namespace Client._Classes.Global
{
public class BaseControl : System.Web.UI.UserControl
{
...
}
}
It should be noted that several other .ascx controls inherit from BaseControl as well, and there does not appear to be any issues.
I think that the problem is that your namespace name ends with your class name:
namespace Layouts.layouts_Header
{
public partial class layouts_Header // * snip *
Try removing the layouts_Header part from your namespace and then change the value of the Inherits attribute to Layouts.layouts_Header
It appears that the solution provided to me contained a folder structure as such:
/temp/installation_history/681FBB6D58774C7D96B37D1353B7441E/layouts
which contained copies of Header.ascx and Header.ascx.cs. I discovered these files when I excluded the actual copies from Header.ascx and Header.ascx.cs from my project.
Once I removed the temp folder and all of its contents, the solution compiled. Thanks everyone for the advice.
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