将连接字符串传递给Entity

将连接字符串传递给Entity

本文介绍了将连接字符串传递给Entity Framework 6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类库(数据库优先)中使用EF6



当我遵循向导并添加表时,我选择不将连接字符串存储在应用程序中.config,然后发送连接字符串。



我以前没有做过。通常,我选择将连接字符串放入app.config文件中。



我现在完全不知道如何实际调用函数并将连接字符串传递给它了。 / p>

下面是我希望与解决方案相关的代码段。



在app.config中-EF自动添加以下内容:

 < connectionStrings> 
<添加名称= cerviondemoEntities connectionString = metadata = res://*/DatabaseModel.cervionEDM.csdl | res://*/DatabaseModel.cervionEDM.ssdl | res://*/DatabaseModel.cervionEDM .msl; provider = System.Data.SqlClient; provider连接字符串=&数据源= DEVBOX;初始目录= cerviondemo;用户ID = sa; MultipleActiveResultSets = True; App = EntityFramework& providerName = System.Data.EntityClient />
< / connectionStrings>

我自动生成的上下文类如下:

  // ------------------------------------ ------------------------------------------ 
//<自动生成的
//此代码是从模板生成的。
//
//对此文件进行手动更改可能会导致应用程序发生意外行为。
//如果重新生成代码,对此文件的手动更改将被覆盖。
//< /自动生成>
// -------------------------------------------- ----------------------------------

命名空间CervionFunctions.DatabaseModel
{
使用系统;
使用System.Data.Entity;
使用System.Data.Entity.Infrastructure;

公共部分类cerviondemoEntities:DbContext
{
public cerviondemoEntities()
:base( name = cerviondemoEntities)
{
}

受保护的覆盖无效OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}

公共虚拟DbSet< Customer>客户{组; }
公共虚拟DbSet< Ticket>门票{组; }
}
}

最终,我试图调用以下测试功能:

 公共静态列表< Customer> customerToUpdate()
{
cerviondemoEntities db;

使用(db = new DatabaseModel.cerviondemoEntities())
{
var result =来自db中的客户。客户
选择客户;

返回结果.ToList();
}
}

我不知道如何将连接字符串发送到该功能:(



任何帮助将不胜感激!

解决方案

按照惯例,Entity Framework采用与上下文名称相同的连接字符串。
例如:

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

DbContext类有一个使用连接字符串的构造函数。您可以添加另一个以连接字符串作为参数的构造函数,并将其传递给基本构造函数。

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

请确保创建一个局部类,以免添加的构造函数被覆盖。



示例ConnectionString:

 < connectionSt戒指
< add name = cerviondemoEntities connectionString = data source = server\database;初始目录= catalog;持久安全信息= True;用户ID = user; password =密码; MultipleActiveResultSets = True; App = EntityFramework providerName = System.Data.SqlClient />
< / connectionStrings>


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!

解决方案

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=server\database;initial catalog=catalog;persist security info=True;user id=user;password=password;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>

这篇关于将连接字符串传递给Entity Framework 6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 13:11