0

I need to set my Entity Framework connection string in context;

using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

public partial class DERSANEM_MASTEREntities : DbContext
{
    public DERSANEM_MASTEREntities()
        : base("")
    {
    }

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

    public DbSet<sysdiagrams> sysdiagrams { get; set; }
    public DbSet<T_HATA_LOG> T_HATA_LOG { get; set; }
    public DbSet<T_MUSTERILER> T_MUSTERILER { get; set; }
}

3 Answers 3

1

You can use DbContext constructor and send name of connectionString.

namespace Dersanem
{
    using System;    
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class DERSANEM_MASTEREntities : DbContext
    {
        public DERSANEM_MASTEREntities()
            : base("MyDbConnection")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }    
        public DbSet<sysdiagrams> sysdiagrams { get; set; }
        public DbSet<T_HATA_LOG> T_HATA_LOG { get; set; }
        public DbSet<T_MUSTERILER> T_MUSTERILER { get; set; }
    }
}

More info:

//
// Summary:
//     Constructs a new context instance using the given string as the name or connection
//     string for the database to which a connection will be made. See the class remarks
//     for how this is used to create a connection.
//
// Parameters:
//   nameOrConnectionString:
//     Either the database name or a connection string.
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
public DbContext(string nameOrConnectionString);
Sign up to request clarification or add additional context in comments.

Comments

0

You can modify the context class constructor, and pass the parameter of your string connection:

public DERSANEM_MASTEREntities(bool con = true) : base(ConnectionStringClass.GetConnectionString())
        {...
}

o:

public DERSANEM_MASTEREntities()
        : base("GetConnectionStringName")
    {...
    }

1 Comment

GetConnectionString is just a static method that returns a connection string .
0

thank you for Answers

I Solved this problem

public partial class DERSANEM_MASTEREntities : DbContext
{
    public DERSANEM_MASTEREntities()            
    {
        this.Database.Connection.ConnectionString = CS_Sifreleme.Decrypt("TcF8EGfKWDbCdqgS.........");
    }

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

    public DbSet<sysdiagrams> sysdiagrams { get; set; }
    public DbSet<T_HATA_LOG> T_HATA_LOG { get; set; }
    public DbSet<T_MUSTERILER> T_MUSTERILER { get; set; }
}

1 Comment

This is same as the answers. So I think the one of the other answers deserve to be accepted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.