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="data source=CCS-6YZ8F72\CONQUESTDB;initial catalog=ConquestDB;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework"" 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?
EFthe 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 themsdndocumentation on the differences