مرجع تخصصی برنامه نویسان

انجمن تخصصی برنامه نویسان فارسی زبان

کاربر سایت

ehsani2013

عضویت از 1394/12/06

تعريف connection string سفارشي براي generic repository در ,مدل UnitOfWork

  • شنبه 26 فروردین 1396
  • 13:03
تشکر میکنم

با سلام خدمت اساتيد 

من generic repository و UnitOfWork رو توي يه پروزه WinForm اجرا كردم حال ميخوام connection String رو خودم سفارشي داخل كد بدم نه اينكه از فايل App.Config بخونه . لطفا راهنمايي بفرماييد . 

با تشكر .

پاسخ های این پرسش

تعداد پاسخ ها : 10 پاسخ
کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • شنبه 26 فروردین 1396
  • 13:11
public partial class MyDatabaseEntities : DbContext
{
public MyDatabaseEntities(string connectionString)
    : base(connectionString)
{
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    throw new UnintentionalCodeFirstException();
}

public virtual DbSet<MyTable> MyTable { get; set; }

string myConnectionString = "...";
MyDatabaseEntities = new MyDatabaseEntities(myConnectionString);

کاربر سایت

ehsani2013

عضویت از 1394/12/06

  • شنبه 26 فروردین 1396
  • 14:26

سلام آقاي مدائني عزيز 

اون دو خط آخر رو بايد تو كدوم كلاس استفاده كنم ؟ 

کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • شنبه 26 فروردین 1396
  • 14:29

سلام دوست من

unit Of Work

کاربر سایت

ehsani2013

عضویت از 1394/12/06

  • شنبه 26 فروردین 1396
  • 14:45

اين پيام بلند بالا رو زحمت كشيد داد : smiley

An exception of type 'System.Data.Entity.Infrastructure.UnintentionalCodeFirstException' occurred in DAL.dll but was not handled in user code

Additional information: The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development.  This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715

کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • شنبه 26 فروردین 1396
  • 18:30

کدی که نوشتید را قرار دهید

کاربر سایت

ehsani2013

عضویت از 1394/12/06

  • یکشنبه 27 فروردین 1396
  • 08:16

سلام مجدد خدمت مهندس عزيز 

من از روش Database First استفاده كردم . كدهاي داخل UnitOfWork من : 

namespace DAL
{
    public class UnitOfWork : IDisposable
    {
        private static string conn = "Data Source=.;Initial Catalog=SaatSchool;Persist Security Info=True;";
        SaatSchoolEntities db = new SaatSchoolEntities(conn);

        private GenericRepository<tbl_Peoples> _peopleRepository;
        private GenericRepository<tbl_Group> _groupRepository;
        private GenericRepository<tbl_TemplateMsg> _templateMsgRepository;
        private GenericRepository<tbl_ConfClock> _confClockRepository;
.
.
.

اينم ابتداي فايل Generic

namespace DAL.Generics
{
    public class GenericRepository<TEntity> where TEntity : class
    {
        private SaatSchoolEntities _context;
        private DbSet<TEntity> _dbSet;

        public GenericRepository(SaatSchoolEntities context)
        {
            _context = context;
            _dbSet = context.Set<TEntity>();
        }

الان خطايي كه داره ميده فك كنم داره فحش ميده . wink (ميگه يا از DatabaseFirst استفاده كن يا از CodeFirst)

کاربر سایت

ehsani2013

عضویت از 1394/12/06

  • یکشنبه 27 فروردین 1396
  • 08:26

اين از Context :

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Data.Entity.Core.EntityClient;

namespace DAL
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    
    public partial class SaatSchoolEntities : DbContext
    {
        public SaatSchoolEntities(string connectionString)
            : base(connectionString)//("name=SaatSchoolEntities")
        {
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
    
        public virtual DbSet<tbl_Group> tbl_Group { get; set; }

کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • یکشنبه 27 فروردین 1396
  • 08:37

خب از اول بگید DBFirst استفاده کردید


                Entities db=new Entities();
                db.Database.Connection.ConnectionString = "";

کاربر سایت

ehsani2013

عضویت از 1394/12/06

  • یکشنبه 27 فروردین 1396
  • 09:09

پوزش كه نگفتم ولي من از هر دوش استفاده كردم . dbFirst رو آوردم روي UnitOfWork و جنريك . الان با توضيح شما باز هم خطا دارم چون اگه كد بالا رو تو كانستراكتور unitofwork هم كه تعريف مي كنم نميره ازش بخونه و اول ميره سراغ app.config و چون توش connection string نيست خطا ميگيره !

کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • یکشنبه 27 فروردین 1396
  • 09:14

نباید ابنطور باشه

باید ابتدا از کد بخونه

سرچ کنید Set Connection String EF In Code

کاربرانی که از این پست تشکر کرده اند

هیچ کاربری تا کنون از این پست تشکر نکرده است

اگر نیاز به یک مشاور در زمینه طراحی سایت ، برنامه نویسی و بازاریابی الکترونیکی دارید

با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)