2

I have an ASP.NET MVC 4 web app and I'm defining connection strings in my web.config. I'm using SimpleMembership which by default wants to use a connection string with the name DefaultConnection

I'm also using EntityFramework 5 to access the same database and store my app data. My EntityFramework context is called AppContext. Therefore, my context wants to by default use a connection string with the name AppContext.

My current solution is to define two connection strings:

<add name="DefaultConnection" connectionString="REMOVED" providerName="System.Data.SqlClient" />
<add name="AppContext" connectionString="REMOVED" providerName="System.Data.SqlClient" />

What's the commonly used solution here? I assume this is a common scenario and people are either overriding the default name of the EntityFramework connection string or the SimpleMembership connection string.

4

1 回答 1

0

哼,轮到我提问了:谁是老大?:-)

您不必接受默认设置,除非它们确实符合您想要做的。

我不明白为什么你需要有 2 个完全相同的连接字符串到同一个数据库!我确信它可以这样工作,但是让任何一个使用另一个连接字符串并不是什么大问题。例如,默认情况下,在 AccountModels 中,我有:

public class UsersContext : DbContext
{
    public UsersContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<webpages_Membership> Membership { get; set; }
}

我只需要在其中将“DefaultConnection”更改为“AppContext”即可使用与实体框架相同的连接字符串来使用 SimpleMembership:

public class UsersContext : DbContext
{
    public UsersContext()
        : base("AppContext")
    {
    }

    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<webpages_Membership> Membership { get; set; }
}
于 2013-09-30T15:25:20.933 回答