3

My connection string is as follows:

<connectionStrings>
<add name="InventoryContext" connectionString="metadata=res://*/Database.InventoryModel.csdl|res://*/Database.InventoryModel.ssdl|res://*/Database.InventoryModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=CCS-6YZ8F72\CONQUESTDB;initial catalog=ConquestDB;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
<add name="InventoryContextIdentity" providerName="System.Data.SqlClient" connectionString="Data Source=CCS-6YZ8F72\CONQUESTDB;Initial Catalog=ConquestDB;Integrated Security=True;MultipleActiveResultSets=True;Application name=EntityFramework;" />
</connectionStrings>

For entity I have the following:

namespace ConquestInventory.Database
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class InventoryContext : DbContext
    {
        public InventoryContext()
            : base("name=InventoryContext")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<C__MigrationHistory> C__MigrationHistory { get; set; }
        public virtual DbSet<AspNetRole> AspNetRoles { get; set; }
        public virtual DbSet<AspNetUserClaim> AspNetUserClaims { get; set; }
        public virtual DbSet<AspNetUserLogin> AspNetUserLogins { get; set; }
        public virtual DbSet<AspNetUserRole> AspNetUserRoles { get; set; }
        public virtual DbSet<AspNetUser> AspNetUsers { get; set; }
        public virtual DbSet<Table_Asset_Cert> Table_Asset_Cert { get; set; }
        public virtual DbSet<Table_Asset_Info> Table_Asset_Info { get; set; }
        public virtual DbSet<Table_Asset_Part> Table_Asset_Part { get; set; }
    }
}

And for identity I have the following:

namespace ConquestInventory.Models
{
    public class MyIdentityDbContext : IdentityDbContext<MyIdentityUser>
    {
        public MyIdentityDbContext() : base("name=InventoryContextIdentity", throwIfV1Schema: false)
        {

        }

        public static MyIdentityDbContext Create()
        {
            return new MyIdentityDbContext();
        }
    }

    public class MyIdentityUser : IdentityUser
    {
        public string FullName { get; set; }
    }

    public class MyIdentityRole : IdentityRole
    {
        public MyIdentityRole()
        {

        }

        public MyIdentityRole(string roleName, string description) : base (roleName)
        {
            this.Description = description;
        }

        public string Description { get; set; }
    }
}

This works just fine. The thing I do not like is that both InventoryContext and InventoryContextIdentity point to the same database. The first one is System.Data.EntityClient, the second is System.Data.SqlClient.

Now if I delete the InventoryContextIdentity and use the InventoryContext for both, the identity login/register will not work saying MyIdentityUser is not available in this model. Vise versa, if I delete the InventoryContext, it says that I have to use the System.Data.EntityClient for entity database calls for the rest of my code.

I can leave it like this but was wondering if there was a easy way to only have one connection string. Will this be a problem when I put it on a server and have to connect to the database, will I have to connect twice?

3
  • You're going to have to connect to it multiple times anyway, even if you use a single connection string. Commented Feb 26, 2016 at 15:48
  • when you connect to sql server via EF the connection strings format is totally different and there should be no issue having both connection strings in the config file.. I have seen projects where there have been 4 different database connection strings 3 for SQL server System.Data.SqlClient and 1 for System.Data.EntityClient. perhaps you you could read the msdn documentation on the differences Commented Feb 26, 2016 at 15:48
  • Why not do the obvious and have a single context? Have your InventoryContext inherit the identity context. odetocode.com/blogs/scott/archive/2014/01/03/… Commented Feb 26, 2016 at 15:59

1 Answer 1

1

Yes, you can use just one connection string, but then you will need to pass to the DBContext the connection string manually on each instantiation.

A good idea can be to create the connection string at the static constructor of your main class or on a configuration class and then use it everywhere:

public static class MyConfig
{
    public static string ECS;

    static MyConfig()
    {
        string srcConnectionString = System.Configuration.ConfigurationManager.
             ConnectionStrings["connectionStringName"].ConnectionString;

        EntityConnectionStringBuilder sb = new EntityConnectionStringBuilder(srcConnectionString);

        ECS = sb.ToString();
    }
}

Then, when you need to use a DBContext you instantiate it like this:

MyDBContext context = new MyDBContext(MyConfig.ECS);

Anyway, EF will manage it's own connections, you don't have a full control of the life cycle of the connections as they're pooled, reused and destroyed by internal policies unless you use the Connection.Open() and Connection.Close() functions on the context (which is never a good idea).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.