簡體   English   中英

我在web.config中需要兩個連接字符串嗎?

[英]Do I need two connection strings in web.config?

我的代碼有問題。 我在文件KekantoContext.cs中有此類

  public class KekantoContext : DbContext
{

    public DbSet<Lugar> Lugares { get; set; }
    public DbSet<Categoria> Categorias { get; set; }
    public DbSet<UserMessage> UserMessages { get; set; }

  }

我在AccountModel.cs文件中還有另一個類:

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

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

我想知道是否有必要在我的web.config文件中有2個連接字符串。

我有這個:

    <connectionStrings>
  <add name="KekantoContext"
 connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Kekanto.mdf;Integrated Security=True"
 providerName="System.Data.SqlClient"/>

   <add name="UsersContext"
 connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Kekanto.mdf;Integrated Security=True"
 providerName="System.Data.SqlClient"/>

</connectionStrings>

該代碼不適用於這2個connectionStrings,但是我想知道一個可能的解決方案是否需要2個連接字符串

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

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

我認為可以將tor參數的uctwo連接字符串的名稱傳遞給您,他只需將要使用的字符串的名稱傳遞給he字符串。

public UsersContext()
        : base("UsersContext")
    {

    }

或者你可以在運行時做

   public UsersContext(string connect)
            : base(connect)
        {

        }

UserContext =  new UserContext("UsersContext");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM