简体   繁体   English

实体框架基础

[英]Basics of Entity Framework

In my demo solution, I have a two projects DataAccess and WebInterface. 在演示解决方案中,我有两个项目DataAccess和WebInterface。

In DataAccess project, I have one EF for Users and a class file UserDataAccessService.cs 在DataAccess项目中,我有一个用于用户的EF和一个类文件UserDataAccessService.cs

Code in UserDataAccessService.cs:- 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 现在在WebInterface项目中,我只是有一个ManageUsers.aspx页面,我试图在该页面上通过以下代码获取用户列表

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. 要修复此问题,我将其System.Data.Entity.dll包含在已修复此问题的WebInterface项目的参考文件夹中。

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. 如果查看我的代码,则可以将与EF相关的代码包含在DataAccess项目中,该项目中包含与EF相关的类所需的所有必需的dll参考。

and in WebInterface project I have included DataAccess.dll and in it .aspx page I am simply calling it function to the fetch data. 在WebInterface项目中,我包含了DataAccess.dll,在.aspx页面中,我只是将其函数称为获取数据。

Then why it requires System.Data.Entity.dll in WebInterface project too? 那为什么还要在WebInterface项目中也需要System.Data.Entity.dll?

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. 我只有99%的把握,因为您使用的是老式的EF :),并且您没有共享User类,但是...我认为是EntityFramework知道的User类。 Is it the case that User inherits from EntityObject? 用户是否从EntityObject继承?

You can avoid that by mapping data from entity classes to you UI classes. 您可以通过将数据从实体类映射到您的UI类来避免这种情况。 But that takes all the magic from using EF. 但这需要使用EF的所有魔力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM