简体   繁体   中英

Do I need two connection strings in web.config?

I am having problems in my code. I have this class in the file KekantoContext.cs

  public class KekantoContext : DbContext
{

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

  }

And I have another class in the AccountModel.cs file:

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

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

I want to know if it's necessary to have 2 connection strings in my web.config file.

I have this:

    <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>

The code didn't work with these 2 connectionStrings, but I want to know if a possible solution needs 2 connection string

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

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

I think it ok to have tor parameter the name of uctwo connection string you just have to pass in to the he string the name of the string you want to use.

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

    }

or you can do it at runtime

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

        }

UserContext =  new UserContext("UsersContext");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM