none
Basics of Entity Framework RRS feed

  • Question

  • Hi Guys 

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



    Wednesday, April 10, 2013 10:03 AM

Answers