Member Avatar for Member #886280

Hi I am the new in C# and I do some semestral project, I have done generating the entity data model, and I want to connect to the database from class by dbcontext using the ConnectionString. I have problem to bind a connection to these database. Can someone help me?

There are my classes:
1.) this classes throw me warning that is obsolete

public static class ConnectionSettings
    {
        public static String ConnectionString = ConfigurationSettings.AppSettings[@"data source=(localdb)\v11.0;initial catalog=SusibarDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""];

    }

2.)this is the the second: EntityContainer is the name in configuration of xml

ublic class ItemDbContext : DbContext
    {
         public ItemDbContext()
             : base("EntityContainer")
         { }

         public DbSet<Item> Items { get; set; }
    }

3.)this is the class to adding data into database

public class RequestModel
    {

        private EntityDbContext.ItemDbContext dbContext = new EntityDbContext.ItemDbContext();

        public int GetUnifishedRequestsCount() { return 5; }

        public void TestInsert() 
        {
            Item i = new Item();
            i.Id = 1;
            i.Name = "Test";
            i.Created = DateTime.Now;
            i.Deleted = false;

            dbContext.Items.Add(i);

            dbContext.SaveChanges();
        }
    }

4.)the main

class Program
    {
        static void Main(string[] args)
        {
            model.RequestModel model = new model.RequestModel();

            model.TestInsert();

            Console.WriteLine("...");
            Console.ReadLine();

        }
    }

Dani AI

Generated

— the warning is because ConfigurationSettings is obsolete and the connection string is being handled incorrectly. Put the connection string into the app/web.config under <connectionStrings> and read it with ConfigurationManager (add a reference to System.Configuration). 's note about SqlConnection is useful only if switching to raw ADO.NET or if you want to create a SqlConnection and pass it into EF; EF will open/close connections automatically in normal use.

Example connection-string patterns (Database‑First EDmx needs an EntityClient metadata string; Code‑First uses a plain SQL string):

<connectionStrings>
  <add name="MyModelEntities" providerName="System.Data.EntityClient"
       connectionString="metadata=res://*/Models.MyModel.csdl|res://*/Models.MyModel.ssdl|res://*/Models.MyModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MyDb;Integrated Security=True;MultipleActiveResultSets=True;&quot;" />
</connectionStrings>
<connectionStrings>
  <add name="MyDb" providerName="System.Data.SqlClient"
       connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MyDb;Integrated Security=True;MultipleActiveResultSets=True;" />
</connectionStrings>

Typical usage patterns (different from the code in the thread):

// use name= to force lookup in <connectionStrings>
using(var ctx = new MyModelContext("name=MyModelEntities"))
{
    ctx.MyEntities.Add(new MyEntity { Name = "Test", CreatedUtc = DateTime.UtcNow });
    ctx.SaveChanges();
}

Or pass an explicit SqlConnection into the context:

var sql = ConfigurationManager.ConnectionStrings["MyDb"].ConnectionString;
using(var conn = new SqlConnection(sql))
using(var ctx = new MyDbContext(conn, contextOwnsConnection: true))
{
    // ...
}

Quick troubleshooting checklist:

  • Ensure the config file is the one copied to the program output (ConsoleApp.exe.config).
  • Match the connection-string name exactly to what the context expects (or use name=...).
  • For EDMX, providerName must be System.Data.EntityClient and metadata=... present.
  • Don’t set identity PK values manually if the DB generates them.
  • Wrap SaveChanges in try/catch and inspect InnerException for SQL/EF errors.
  • Note LocalDB instance names vary: (localdb)\v11.0 (older) vs (localdb)\MSSQLLocalDB (newer).

no SqlConnection? using System.Data.SqlClient;

Member Avatar for Member #886280

OK i will use SqlConnection it was my first choice but i use that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.