与实体框架连接的不同方式

本文关键字:方式 连接 实体 框架 | 更新日期: 2023-09-27 18:14:01

关于如何与实体框架进行数据库连接而不是在web.config中使用连接字符串的另一种方式。可能将参数传递给modelBuilder或DBContext????

与实体框架连接的不同方式

我相信您可以将参数中的连接字符串传递给DataContext。你试过什么?为什么要这样做?查看此链接

这可能是有用的:在MyEntities的源代码:

public partial class MyEntities : ObjectContext
{
    #region Constructors
    /// <summary>
    /// Initialize a new MyEntities object.
    /// </summary>
    public MyEntities(string connectionString) : base(connectionString, "MyEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }
    /// <summary>
    /// Initialize a new MyEntities object.
    /// </summary>
    public MyEntities(EntityConnection connection) : base(connection, "MyEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }
#endregion
....

EDIT根据此Q&A my EF 4 (4.1.10331.0)

和这里它看起来像我的网。config for EF:

<add name="MyEntities" connectionString="metadata=res://*/Models.MyModel.csdl|res://*/Models.MyModel.ssdl|res://*/Models.MyModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.'SQLEXPRESS;Initial Catalog=MyDb;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

查看此主题的第三篇文章:

http://social.msdn.microsoft.com/forums/is/adodotnetdataservices/thread/2eb0e7a8 - 10 - c5 - 4 - c6c - 80 - b8 - 23 - cb39161345

protected override AdventureWorksEntities CreateDataSource()
    {
        EntityConnection entityConnection = new EntityConnection();
        entityConnection.ConnectionString = "ConnectionStringConnecting to the  databaseName";
        //set other proeprties on the entityConnection
        AdventureWorksEntities dataContext = new AdventureWorksEntities(entityConnection);
        return dataContext;
     }