Create New Table In SQL Via Entity Framework - 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.

Related

Is it possible to efficiently change table and column names, using prisma?

I want to use prisma on an existing database. Now that I'm researching it, I realise that all my tables and columns are incorrectly named. I would like to correct the names, but I don't want to lose my data.
I did a prisma "pull" so I have a schema of my current database. But then I don't quite understand how (if possible) do I rename my tables and columns using the schema.
What are my options? Can I change the schema.prisma and make prisma take care of all the "alter" statements? How do I do this?
It's not natively supported yet. There is a Feature Request to handle renames effectively.
You can use this technique to rename your tables and fields without dropping the data.
Create an empty migration file with prisma migrate dev --create-only
Add sql: ALTER TABLE IF EXISTS "OldName" RENAME TO "NewName";
Change table name in schema.prisma file
Run prisma migrate dev

Create a new table with existing style from layer_styles table in postgres

Is there any way to create a postgis table with existing style from layer_styles table? Say for example i have so many styles stored in layer_styles table. I need to assign one of the style from layer_styles table to the new table which i am going to create. Can that be done using postgresql during table creation using sql command?
You need to identify the layer IDs of interest (or name, or any column you want) and to create the new table using this data+structure. However using the style in this secondary table may not be that easy
create table public.layer_styles_2 as
(select * from public.layer_styles where id in (2,3));

how can I rename a table / move to a different schema in sql DB2?

I am trying to rename a table in db2 like so
rename table schema1.mytable to schema2.mytable
but getting the following error message:
the name "mytable" has the wrong number of qualifiers.. SQLCODE=-108,SQLSTATE=42601
what is the problem here.... I am using the exact syntax from IBM publib documentation.
You cannot change the schema of a given object. You have to recreate it.
There are severals ways to do that:
If you have only one table, you can export and import/load the table. If you use the IDX format, the DDL will be included in the generated file. If using another format, the table has be created.
You can recreate the table by using:
Create table schema2.mytable like schema1.mytable
You can extract the DDL with the db2look tool
If you are changing the schema name for a schema given, you can use ADMIN_COPY_SCHEMA
These last two options only create the table structure, and you still need to import the data. After having create the table, you insert the data by different ways:
Inserting directly
insert into schema2.mytable select * from schema1.mytable
Via load from cursor
Via a Load or import from file (The file exported in the previous step)
The problem is the foreign relations, because they have to be recreated.
Finally, you can create an alias. It is easier, and you do not have to deal with relations.
You can easily rename a table with this statement:
RENAME TABLE SCHEMA.TABLENAME TO NEWTABLENAME;
You're not renaming table in provided example, you're trying to move to different schema, it's not the same thing. Look into db2move tool for this.
if you want to rename a table in the same schema, you can use like this.
RENAME TABLE schema.table_name TO "new_table_name";
Otherwise, you can use tools like DBeaver to rename or copy tables in a db2 db.
What if you leave it as is and create an alias with the new name and schema.
Renaming a table means to rename a table within same schema .To rename in other schema ,db2 call its ALIAS:
db2 create alias for

PostgreSQL: How to delete dynamically created table using SQL

I am developing a windows application and using Postgres as backend database. At some point in my application i am dynamically creating table e.g Table1, then Table2 and so on. In this way i have many dynamic table in my database. Now i provide a button "Clean Database", so i need to remove all those dynamic tables using SQL query. Should some one guide me how to write SQL Query that automatically delete all such tables?
You should just be able to say
DROP TABLE {tablename}
for each dynamically created table. Try that and see if it works.

how to create a EF data model like a sql server view

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.