BradleyH
BradleyH

Reputation: 1

Entity Framework: How to Treat dbo.tablename diffrent from obd.tablename

I have website1 and webiste2 that runs against MyDatabase.

In MyDatabase I have some tables: dbo.Users, dbo1.Posts, dbo2.Posts. There is a foreign key relation ship between dbo.Users and dbo1.Posts, and dbo.Users and dbo2.Posts.

I want website1 and website2 to run the same code base, is it possible to have website one get it's Posts from dbo1.Posts, while website2 gets their posts from dbo2.Posts?

The thought is that the desired schema (dbo1 or dbo2) would come from a configuration file.

In my situation, the information in dbo1.Posts and dbo2.Posts could be considered confidential. There may be users that might have access to both, or there might be users that have access to one. So as I see it there are two approaches, separate the databases physically, or logically. My Task at hand is to investigate if it can be done logically or not

Upvotes: 0

Views: 173

Answers (5)

Anthony Aziz
Anthony Aziz

Reputation: 135

Here's an alternative solution if you want to dynamically set your schema name when you create the context... basically similar to @BradleyH 's answer, but instead of reading from a config class, you take the schema name as arguments to your mapping methods/constructors.

See here for a more detailed explanation: https://stackoverflow.com/a/14782001/243607

Upvotes: 0

BradleyH
BradleyH

Reputation: 1

Okay so here is what ended up happening. Still not sure if it's the best solution, but if anybody comes looking for the answer to this question I did figure out a way to make it work.

With that said I still agree with Matt, and Hacked that there should be a simpler way to do this.

There are three diffrent pices to my puzzel.

First there was a map for the entity to table in question where I used the following:

this.ToTable("Posts", ConfigurationHelper.GetSiteSchema());

Second I had a line in a Configuration helper that read from the AppSettings in the Web.config file

    public static string GetSiteSchema()
    {
        return ConfigurationManager.AppSettings["SiteSchema"];
    }

Third in the web.config file I had added an Appsettings as follows:

 <appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="SiteSchema" value="dbo1"/>
  </appSettings> 

After building the app and running it I get the posts from dbo1.Posts. I can then switch the string in the web.config file to "dbo2", refresh the page and see the posts from dbo2.Posts.

Upvotes: 0

mj-gholami
mj-gholami

Reputation: 883

Six step:

  1. Use the Type Library Importer to generate an interop assembly for your ActiveX EXE.
  2. Add reference to the interop assembly in your C# project.
  3. Use Process.Start to launch the ActiveX EXE.
  4. Use Marshal.GetActiveObject to obtain the running instance of the object in the running ActiveX EXE (equals to GetObject in VB).
  5. Cast the object returned in the previous step to the equivalent type defined in the interop assembly.
  6. Do whatever need to be done against the object.

Upvotes: 1

moribvndvs
moribvndvs

Reputation: 42497

This link explains how to specify the schema for a class using code-first mapping. If you can't refactor your application to not use the same class/table in two different schemas, I'm afraid you'll have to define two Post classes: Post1 that maps to dbo1, and Post2 that maps to dbo2. Additionally, you'll run into impedance when trying to get posts for a User, where you'd have to have User.Post1s and User.Post2s (crude example, sorry).

So unless you have no choice, I'd refactor this to work around the duplicate tables.

Upvotes: 1

Matt
Matt

Reputation: 26971

Honestly, I would re-evaluate your design and just add a column to the Post table indicating the source. Much simpler and cleaner.

Upvotes: 0

Related Questions