"不支持指定的方法"在实体框架中

时间:2012-07-31 06:51:05

标签: c# .net entity-framework entity-framework-4 exception-handling

这是我的实体类:

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");

2 个答案:

答案 0 :(得分: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}条目&amp;此连接字符串不是app.config中的整个配置/ 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对象。

答案 1 :(得分:0)

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

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