1

Objective:

Use EF to enter in data from a POST request from an API. The POST request will contain an "ID", that will map the connection string to an enum, which will have the same name connection string name as it has in the Web.config. Create the "base" context object and add the object to the appropriate table.

Note:

I know I can do this using SqlCommand, but I wanted to take a crack at it using entity framework instead, but I hit a wall.


I've used EF for years, but I wanted to make this POST method as global as I can get it. This API will accept numerous requests from numerous different web sites, but all will use the same model. Each websites POST will go into a different database (that's how they are requesting it).

The problem that I foresee is, each "entity" knows what tables it contains. So when one types context.TABLE.Add(object), EF understands that you want to put this "Car" object in the "Car" table (obviously).

Can this be done using a "global" entity???

public class DbConnectionNames
{
    public enum DbConnectionStringNames
    {
        SocHopeHcpEntities = 1,  // "1" is passed into the POST to map
        XXXXEntities = 2,
        ......
    }
}

<add name="SocHopeHcpEntities" connectionString=".........." />
<add name="XXXXEntities" connectionString=".........." />
.....

var professional = new Professional
{
    ....
    ....
};

string connStringContext = Enum.GetName(typeof(DbConnectionNames.DbConnectionStringNames), model.FormId).ToString();
string connectionString = ConfigurationManager.ConnectionStrings[connStringContext].ConnectionString;

using (var context = new ObjectContext(connectionString))
{
    context.Professionals.Add(professional);  // obviously this doesn't work
    context.SaveChanges();
}

EDIT:

My EF is NOT using POCO, but is already based off a DB to begin with. There could be XX number of different databases, all holding the same similar table. I already have a YYYEntities.Context.cs file auto-generated that inherits from DbContext:

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

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

    public virtual DbSet<AreasOfWork> AreasOfWorks { get; set; }
    public virtual DbSet<Professional> Professionals { get; set; }
}
1

2 Answers 2

2

You still need a context that understands what a Professional is. For example:

public class ProfessionalContext : DbContext
{
    public ProfessionalContext(string connectionString)
        : base(connectionString)
    {
        //This line is optional but it prevents initialising the database
        //every time you connect to a new database
        Database.SetInitializer<ProfessionalContext>(null);
    }

    public DbSet<Professional> Professionals { get; set; }
}

And use it like this:

using (var context = new ProfessionalContext(connectionString))
{
    context.Professionals.Add(professional);
    context.SaveChanges();
}
Sign up to request clarification or add additional context in comments.

Comments

1

DavidG's answer showed you how to pass a connection string to a strongly typed context that knows the entity types you are dealing with. That context inherits from DbContext which you can use directly as illustrated below.

It is worth noting the generic way that does not involve a context 'object' that is specific to your database.

For example see how ObjectContext is used here:

System.Data.Objects.ObjectContext oc = new System.Data.Objects.ObjectContext("connection string");
oc.AddObject("ProfessionalsTable", professional);

Another example is using DbContext:

System.Data.Entity.DbContext dbc = new DbContext("");
dbc.Set(typeof(Professional)).Add(professional);

The generic approach is better if you also do not know which table you want to insert to, so you can also make the object that you want to insert dynamic.

1 Comment

You can also use dbc.Set<Professional>().Add(professional) which looks a bit tidier.

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.