1

We are using two database in that i need to inherit this model or entity table alone. For this how to specify database name for particular class or entity table in entity-frame work. It is possible?

Database Name: "Test1"

public class User : BaseEntity
{        
    public string Email { get; set; }
    public string Password { get; set; }
}

now how to set this model as navigation property in another model. like,

Database Name: "Test2"

public class Test: BaseEntity
{
  public int UserId { get; set; }

  public virtual User User { get; set; }
}

public class BaseEntity
{
   public int Id { get; set; }
}
Wahid Bitar
  • 13,776
  • 13
  • 78
  • 106
Thulasiram
  • 8,432
  • 8
  • 46
  • 54
  • Your question is somewhat confusing. Maybe you should try to show, in pseudo-code perhaps, what you're trying to accomplish? – J. Steen Jul 17 '12 at 12:16
  • @steen: now i edited the question u can see it. – Thulasiram Jul 17 '12 at 12:22
  • That's not inheritance, that's a navigation property. What are you trying to accomplish by doing this, and what is your problem? =) – J. Steen Jul 17 '12 at 12:24
  • @steen: i need to get that User object automatically. it is possible? – Thulasiram Jul 17 '12 at 12:34
  • Is the data represented by the User object stored in a different database than, in this case, Test? – J. Steen Jul 17 '12 at 12:35
  • 1
    yes. user table is in another database i need to get that user object. – Thulasiram Jul 17 '12 at 12:36
  • Then no, it can't be done automatically. You could set up a linked server in the SQL database, and create a view that points to your User table. For example. – J. Steen Jul 17 '12 at 12:37
  • 1
    Actually. This answer might help you: http://stackoverflow.com/questions/6036357/making-an-entity-framework-model-span-multiple-databases – J. Steen Jul 17 '12 at 12:38
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13995/discussion-between-thulasiram-and-j-steen) – Thulasiram Jul 17 '12 at 12:39

2 Answers2

2

Entity Framework DbContext could handle one Database. Then you should have two DbContext's for your two Databases.


Update :

I advice you not to do that. in another words you your Database and your Model should be in harmony "I love this word :)"

And anything beyond that I suppose it should be in Service class "Layer".

So in your example you suppose to have a UserService class the get your User and don't do this logic in the Test class.

One another thing to notice: If your model connected together like this way then you should have just one Database.

Wahid Bitar
  • 13,776
  • 13
  • 78
  • 106
0

If you want to inherit your class from the User class, this is how it is

public class Test: User 
{
   public int UserId { get; set; } 
}

Now Test class will have all the properties of the base class (User) along with it's own property UserId

Shyju
  • 214,206
  • 104
  • 411
  • 497