ورود

View Full Version : مشکل با EF در برنامه نویسی سه لایه



xsbehx
پنج شنبه 28 خرداد 1394, 01:11 صبح
وقتی می خوام با EF تو معماری سه لایه یک Select ساده بنویسیم با خطای زیر مواجه می شم:
132359

این کد App.Config تو لایه DAL هست


<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrame workSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="tehranMatabEntities" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=tehranMatab;integrated security=True;MultipleActiveResultSets=True;App=En tityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectio nFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>


این هم کدهای config تو لایه UI هست


<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>

ali_md110
پنج شنبه 28 خرداد 1394, 01:57 صبح
وقنی با برنامه سه لایه کار میکنید هنکام اجرای برنامه رشته اتصالی که در لایه UI هست فراخانی و استفاده میشه شما دارید از کانکشن لایه غیر از UI یا DAL اشتفاده میکنید
شما باید کانگشن استرینک رو به فایل کانفیگ درون پروژه UI انتقال بدید

xsbehx
پنج شنبه 28 خرداد 1394, 02:38 صبح
خیلی ممنون

کدهای فایل config لایه Ui من شد این :


<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<connectionStrings>
<add name="tehranMatabEntities" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=tehranMatab;integrated security=True;MultipleActiveResultSets=True;App=En tityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>


اون مشکل به کمک شما رفع شد
الان یک خطای دیگه می ده:

An exception of type 'System.Data.Entity.Core.MetadataException' occurred in mscorlib.dll but was not handled in user code

Additional information: Schema specified is not valid. Errors:

Model.ssdl(2,2) : error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.


نمی دونم شاید کدهای لایه DAL اشکال داشته باشه. اونها رو هم می زارم:


tehranMatabEntities db = new tehranMatabEntities();
List<Users_Model> list = new List<Users_Model>();
foreach (var item in db.spUsers_SelectAll().ToList())
{
Users_Model _entity = new Users_Model();
_entity.fldUserId = Convert.ToInt32(item.fldUserId.ToString());
_entity.fldFullName = item.fldFullName.ToString();
_entity.fldEmail = item.fldEmail.ToString();
_entity.fldUsername = item.fldUsername.ToString();
_entity.fldPassword = item.fldPassword.ToString();
_entity.fldTypeId = Convert.ToInt32(item.fldTypeId.ToString());
list.Add(_entity);
}

xsbehx
پنج شنبه 28 خرداد 1394, 03:47 صبح
یکم که بررسی کردم رسیدم اینجا (http://stackoverflow.com/questions/18455747/no-entity-framework-provider-found-for-the-ado-net-provider-with-invariant-name)فهمیدیم که اصلا مشکل از کد نیست. مشکل از EF که روی سیستم من نصب بود
با Nuget نصبش کردم مشکل بر طرف شد.
حتی همین مشکل ابتدای صفحه هم خودش حل شد. یعنی نسخه جدید که نصب شد خودش اومد تو لایه UI فایل config ساخت.
در هر صورت خیلی ممنون که کمک کردید

asiatec
یک شنبه 08 مرداد 1396, 13:21 عصر
وقتی شما ef رو لایه ui نصب میکنی این برنامه شما همچنان چند لایه حساب میشه؟