1

这是我的实体类:

public partial class NerdDinnerEntities : ObjectContext
{
    public NerdDinnerEntities(string connectionString)
        : base(connectionString, "NerdDinnerEntities")
    {
        try
        {
            ObjectContext oc = new ObjectContext(connectionString);

           oc.Connection.ChangeDatabase("NERDDINNER1");
           oc.AcceptAllChanges();
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
        catch (Exception ex) { }
    }

    partial void OnContextCreated();

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    public ObjectSet<Dinner> Dinners
    {
        get
        {
            if ((_Dinners == null))
            {
                _Dinners = base.CreateObjectSet<Dinner>("Dinners");
            }
            return _Dinners;
        }
    }
    private ObjectSet<Dinner> _Dinners;

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    public ObjectSet<RSVP> RSVPs
    {
        get
        {
            if ((_RSVPs == null))
            {
                _RSVPs = base.CreateObjectSet<RSVP>("RSVPs");
            }
            return _RSVPs;
        }
    }

    private ObjectSet<RSVP> _RSVPs;

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    public ObjectSet<sysdiagram> sysdiagrams
    {
        get
        {
            if ((_sysdiagrams == null))
            {
                _sysdiagrams = base.CreateObjectSet<sysdiagram>("sysdiagrams");
            }
            return _sysdiagrams;
        }
    }

    private ObjectSet<sysdiagram> _sysdiagrams;

    /// <summary>
    /// Deprecated Method for adding a new object to the Dinners EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
    /// </summary>
    public void AddToDinners(Dinner dinner)
    {
        base.AddObject("Dinners", dinner);
    }

    /// <summary>
    /// Deprecated Method for adding a new object to the RSVPs EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
    /// </summary>
    public void AddToRSVPs(RSVP rSVP)
    {
        base.AddObject("RSVPs", rSVP);
    }

    /// <summary>
    /// Deprecated Method for adding a new object to the sysdiagrams EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
    /// </summary>
    public void AddTosysdiagrams(sysdiagram sysdiagram)
    {
        base.AddObject("sysdiagrams", sysdiagram);
    }
}

这些是我的 web.config 文件

<add name="NerdDinnerEntities" connectionString="metadata=res://*/Models.NerdDinner.csdl|res://*/Models.NerdDinner.ssdl|res://*/Models.NerdDinner.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=@;Database=NERDDINNER;User ID=@;Password=@@@;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClients" />

我收到错误:

不支持指定的方法

在这一行:

oc.Connection.ChangeDatabase("NERDDINNER1");
4

2 回答 2

2

如果您在MSDN上查找文档,您会发现该方法实际上不受支持。它必须是未来改进或其他东西的占位符。

为那些想要在运行时更改数据库的人扩展:

1.在您的设置中创建一个条目以代替 app.config 中的默认值。将用户名、密码、目录名称(数据库名称)、服务器等详细信息提取到其他设置条目中。

<Setting Name="EntityConnectionString2" Type="System.String" Scope="Application">
  <Value Profile="(Default)">metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source={0};initial catalog={1};persist security info=True;user id={2};password={3};encrypt=True;trustservercertificate=True;multipleactiveresultsets=True;App=EntityFramework"</Value>
</Setting>

请注意 {0}..{3} 条目,并且此连接字符串不是 app.config 中的整个 configuration/connectionStrings/add 条目

2.使用接受连接字符串的 EF 数据库的重载构造函数之一。

var settings = Properties.Settings.Default;  
string constring = string.Format(settings.EntityConnectionString2, settings.Server, settings.Database, settings.User, settings.Password);
NerdDinnerEntities db = new NerdDinnerEntities (constring);  

3.要在运行时更改,您可以使用不同的目录名称以相同的方式创建不同的对象,或者使用不同的目录名称处理并重新创建 db 对象。

于 2012-07-31T06:56:24.887 回答
0

不支持 EntityConnection 的 ChangeDatabase 方法(http://msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnection.changedatabase.aspx

如果要将数据上下文与另一个数据库一起使用,请创建另一个连接字符串并使用新连接字符串创建数据上下文实例

于 2012-07-31T06:56:14.560 回答