I present to you my situation: I have 2 tables, for example, orders lists and customers; to each order is associated with a customer, the orders table belongs to "MyDepot" database and the customers table belongs to "Main" database.
I've create 2 different contexts, MyDepotContext and MainContext.
How do I map the orders and not customers to the MyDepotContext ?
An how do I map customers and not orders to the MainContext ?
Is it possible by using Fluent API ?
Edited :
I'm using SQL Server on Azure, so i cannot use the "Linked Servers" functionnality since it's only implemented for Managed Azure SQL Database.
An EF DbContext is bound to a single Database Connection. If you have multiple databases and can't use cross-database queries you'll have to have multiple DbContext types and manage any cross-context operations on the client.
Related
I'm implementing the multi-tenant with a shared database. But I met the problem when I want to query a table that belongs to a specific tenant.
For example:
I have a table catalog that has relation to the tenant table, at application logic, I always do a query to check a catalog belongs to a specific tenant or not before having another query to handle catalog logic.
So is there any way to make sure the catalog belongs to the specific tenant without step checking because sometimes I forgot to add this validate step => doing on catalog does not belong to a specific tenant?
I use postgres as database, and sequelize as orm
I think what you are looking for is "Row Level Security". In PostgreSQL it is implemented as Row Security Policies. It allows you to control access to specific rows for specific users.
If your tenants connect to the database using their DB users you can apply a policy:
CREATE POLICY tenants_catalog ON catalog
USING (tenant = current_user);
JPA: is there a way to map some entities to a schema of another database instance? e.g.,
#Entity
public class Foo {
}
#Entity
#Table(schema="schema1")
public class Bar {
}
The Bar entity is mapped to the schema1 of the same database instance. Is there a way in JPA to map it to a schema in a remote database instance? It is useful for sharing entities among multiple applications.
Can the "catalog" be used for this purpose?
What do you mean by 'remote database'?
If you use #Table(schema = "myschema", name = "bar"), Hibernate will qualify all queries with the schema name (e.g. SELECT e FROM Bar will ultimately translate to SELECT * FROM myschema.bar). If the database user you're using to connect to the DB has access to myschema.bar (whatever such a DB object is), then the query will work; if not, then the query will fail.
If you mean 'a remote DB that is a separate server', then, of course, you can only connect to the DB using one JDBC connection per persistence context. If that's your scenario, perhaps you should consult the docs of the RDBMS for ways to connect two DB instances (in Oracle, for example, you could use database links and synonyms).
Make sure that you understand the implications, though, as such a solution introduces its own class of problems (including the fact that you suddenly have implicit distributed transactions in your system).
As a side note, I'm not sure how such an approach is 'useful for sharing entities among multiple applications' or why one would even think 'sharing entities among multiple applications' is somehow useful, but I'd seriously think through the idea of integrating multiple application via shared/linked DBs. It usually introduces more problems than it solves.
If I understand well what you mean, you should use two (or more) different persistence context
I am looking for a way to map stored procedures logically related to a table as functions within the Entity generated for that specific table instead of function in overall Context.
As an example, I have generated edmx out of existing bank database and I got a Context and several entities corresponding to the tables. Assume we have tables for Account, Transactions, Address, etc... and I have a stored proc returning current balance for the account. I want to map this stored proc to the Account entity instead of the context. This will help me to call Account.GetBalance() instead of Context.GetBalance().
Is this possible in entity framework ? I did lot of search and read few articles/blogs in msdn but couldn't find any solution for this.
No it is not possible unless you manually wrap the method exposed on context in your entity - that would make your entities persistence aware and EF dependent which is currently considered as exact opposite of design people should follow.
In terms of EF balance should be property of account entity and retrieving balance should mean reloading account or executing projection query (or your procedure) on accounts set / context. Queries are executed from context and stored procedures as well.
I am writing an application that will move data from one database to another Using JPA EntityManager API. My questions are : 1. Can i use multiple entity managers in a single transaction? 2. Can i read an entity from one database and persist it in the other? what are the issues that am likely to encounter?
Can I use multiple entity managers in a single transaction?
Yes, using JTA. I'm not sure you need a global transaction in your case though. Are you really moving or copying entities from one DB to the other? In the later case, you could use two transactions sequentially.
Can I read an entity from one database and persist it in the other?
Assuming they have the same structure and you don't have any conflicting PK, it should be possible to read an entity using a first entity manager, detach it and then merge it using another entity manager. If you have possible PK conflicts, you'll have to use a DIY approach (vs a simple merge).
I have a database with a table for active orders and one for inactive orders. I would like to model this in Entity Framework as one entity called Orders. I also need a way to determine if an order in this collection is active or not, preferably by having a status property on the entity that is set according to what table it is in. Is there anyway to do this using Entity Framework 1. What about in Entity Framework 4?
You could create a view in your DB and generate the entity from that.
Take a look at the Table Per Concrete Type inheritance.
It is described here in ADO.NET Team Blog.
I think this is what you are looking for: How to: Define a Model with Multiple Entity Sets per Type (Entity Framework)
"The Entity Data Model (EDM) allows an entity type to be included by multiple entity sets within a single entity container or for an entity type to be included in entity sets in multiple entity containers. Defining multiple entity sets per type (MEST) allows users to streamline their code when databases have partitioning or other such scenarios where multiple tables have the same structure."
If I am understanding you correctly both active and inactive orders would share the same properties (for example: both would have a decimal "amount" property) if this is the case then in EF 1, I am pretty certain this is not possible. I think you will have to fall back to Mapping your entities to a POCO Orders object.
A good way to do one entity that shares multiple tables is to use Entity Splitting. MSDN has a very simple tutorial that walks you through the process which is very easy, however, you may need to reshape your data model: http://msdn.microsoft.com/en-us/data/jj715646.aspx