how to create a EF data model like a sql server view - entity-framework

I need to create a model of union of several sql server tables and i have to get ability of
insert , select , update and delete ...
(id like to use the model as same as any other model)
any suggestions ?
thanks for reading.
Edit: i tried sql server view but got the fallowing error when i want to insert to sql server view:
Msg 4406, Level 16, State 1, Line 1
Update or insert of view or function 'viewName' failed because it contains a derived or constant field.

You need to create database view + stored procedures for insert, update and delete. You will map the view as a new entity and map imported stored procedures to insert, update and delete operations for that entity.
You actually don't need the database view - you can write the query directly to EDMX by using DefiningQuery but it requires manual modification of EDMX. Default EF tools will delete your manual modification once you run Update from database again.
Even with defining query you still need those stored procedures. There is no other way to make entity based on defining query (view is also imported as defining query) updatable.

Related

Is it possible to have database-wide table aliases?

I am about to model a PostgreSQL database, based on an Oracle database. The latter is old and its tables have been named after a 3-letter-scheme.
E.g. a table that holds parameters for tasks would be named TSK_PAR.
As I model the new database, I'd like to rename those tables to a more descriptive name using actual words. My problem is, that some parts of the software might rely on these old names until they're rewritten and adapted to the new scheme.
Is it possible to create something like an alias that's being used for the whole database?
E.g. I create a new task_parameters database, but add a TSK_PAR alias to it, so if a SELECT * FROM TSK_PAR is being used, it automatically refers to the new name?
Postgres has no synonyms like Oracle.
But for your intended use case, views should do just fine. A view that simply does select * from taks_parameters is automatically updateable (see here for an online example).
If you don't want to clutter your default schema (usually public) with all those views, you can create them in a different schema, and then adjust the user's search path to include that "synonym schema".
For example:
create schema synonyms;
create table public.task_parameters (
id integer primary key,
....
);
create view synonyms.task_par
as
select *
from public.task_parameters;
However, that approach has one annoying drawback: if a table is used by a view, the allowed DDL statements on it are limited, e.g. you can't drop a column or rename it.
As we manage our schema migrations using Liquibase, we always drop all views before applying "normal" migrations, then once everything is done, we simply re-create all views (by running the SQL scripts stored in Git). With that approach, ALTER TABLE statements never fail because there are not views using the tables. As creating a view is really quick, it doesn't add overhead when deploying a migration.

DB2 equivalent for CREATE FORCE VIEW

I am in process of migration MaxDB database to DB2. I need to recreate all views, but their definitions contain references to other views and have the same create date and time, so I'm unable to create them in correct order.
Does the DB2 support somehow the CREATE FORCE VIEW statement?
How can I recreate the views in correct order (without creating SQL parser - because I have just String definition of views from Data Dictionary from MAXDB)?
SELECT for MaxDB:
select vd.*, t.createdate, t.createtime from viewdefs vd
join tables t on vd.viewname = t.tablename and vd.owner = t.owner
order by t.createdate, t.createtime
MaxDB Data Dictionary
You don't indicate what DB2 platform you're using. DB2 for LUW has the database configuration parameter auto_reval, which, when set to deferred (default) or deferred_force, allows you to create dependent objects in any order. With deferred_force in effect objects (including views) that cannot be validated at creation time because of missing dependencies will be created "with error" and revalidated when they are first used in a SQL statement.
You can also explicitly revalidate all invalid objects after you create them by calling the system stored procedure SYSPROC.ADMIN_REVALIDATE_DB_OBJECTS().

When I add a column in the database, under what conditions do I need to update my EDMX?

When I add a column in the database, under what conditions do I need to update my EDMX?
To elaborate:
I know if I add a non-nullable field, I need to update the model if I want to write to the database. What if just I want to read?
What if it's a nullable field? Can I both read and write?
What if I were to change the primary key to the new column but the edmx still has the old column as primary?
1) If you want to port an old database, you need to make sure that every table in your database must have a primary key. This is the only requirement for creating the EDMX.
2) If you've added a column in a table at database side, and have not updated edmx, then you'll simply not be able to use that column though EntityFramework.
If you create a non nullable column with no default value, the insert operation will fail with exception "Cannot insert null into column , statement terminated". And the you'll not be able to read values of that column using entityframeowrk, unless you update the edmx.
3) If you've changed the primary key of any table at database side, and if the edmx is not aware of that, your application might create a runtime exception when performing operations with that table.
Remember, Entity Framework creates SQL queries depending upon its knowledge of database(which is defined in EDMX). So if EDMX is incorrect, the resulting SQL queries so generated might lead to problems at runtime.

EF5 - modify generated insert/update statement before SaveChanges()

I need to modify the generated SQL for insert/update operation, before it is sent to database. The required modification is very specific, so I was hoping that there is a way to simply append string to statement.
For example, SQL looks like this (Oracle BTW):
UPDATE TABLE_A
SET DESCRIPTION = "ABC"
WHERE OBJECTID = 1
But I want to append this line (in SET part) to update one more field:
SHAPE = sde.st_geometry('point (18 57)', 4326)
I can't add SHAPE column to EF model, because that is unsupported data type.
Now, is there a way I can modify EF generated SQL statement?
You could move this update to a simple stored procedure that is mapped into your entity data model.

Create New Table In SQL Via Entity Framework

Is there a way to create (or add) a table in existing Database by EF?
You can use ObjectContex.ExecuteStoreCommand (EF4) and execute any SQL command (if you have required permissions in DB). So yes you can create table from "EF". But you will not add this table to mapping.