Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

c# - Passing Connection String to Entity Framework 6

I am using EF6 in a class library (database first)

When I followed the wizard and added my tables I selected not to store the connections string in the app.config and that I would send the connections string.

I haven't done this before. Normally I select to put the connection string in the app.config file.

I am now completely stumped how I actually call a function and pass the connection string to it.

Below are what I hope are relevant code snippets from my solution.

In the app.config - EF automatically added this:

<connectionStrings>
<add name="cerviondemoEntities" connectionString="metadata=res://*/DatabaseModel.cervionEDM.csdl|res://*/DatabaseModel.cervionEDM.ssdl|res://*/DatabaseModel.cervionEDM.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=DEVBOX;initial catalog=cerviondemo;user id=sa;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

My auto generated context class looks like this:

//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

namespace CervionFunctions.DatabaseModel
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

public partial class cerviondemoEntities : DbContext
{
    public cerviondemoEntities()
        : base("name=cerviondemoEntities")
    {
    }

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

    public virtual DbSet<Customer> Customers { get; set; }
    public virtual DbSet<Ticket> Tickets { get; set; }
}
}

Ultimately, I am trying to call the following test function:

public static List<Customer> customersToUpdate()
    {
        cerviondemoEntities db;

        using (db = new DatabaseModel.cerviondemoEntities())
        {
            var result = from customers in db.Customers
                         select customers;

            return result.ToList();
        }
    }

I cannot work out how to send the connection string to that function :(

Any help would be greatly appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

By convention, Entity Framework takes the connection string that has the same name as the context. For example:

public cerviondemoEntities()
    : base("name=cerviondemoEntities")
{
}

The DbContext class has a constructor that takes a connection string. You can add another constructor that takes a connectionstring as a parameter and pass it to the base constructor.

public cerviondemoEntities(string connectionString) : base(connectionString)
{
}

Be sure to create a partial class so your added constructor is not overwritten.

Sample ConnectionString:

<connectionStrings>
    <add name="cerviondemoEntities" connectionString="data source=serverdatabase;initial catalog=catalog;persist security info=True;user id=user;password=password;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /> 
</connectionStrings>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...