I want to generate EDM from my database file (.mdf), but i don't want to work with all columns. How can i skip these columns from .edmx?
You can't select specific columns at the time of generation. But, if you only want to work with a subset of columns, let the EDM generate the entire table; then in the designer surface you can select the columns you don't need and hit delete to remove them from the model.
One note is that if your database has certain constraints, such as non-nullable columns with no default value, an exception will be thrown if you try to add or update entities without those columns in your model.
You have to do this manually.
Obviously you can map tables as show here, but then you have to go through and manually remove columns. At least this is what I have been told.
Related
I added a new column MyColumn to vwMyView in MyDB. MyDB has a stored procedure MySproc which returns vwMyView.* in a select statement.
When I execute MySproc, vwMyView.MyColumn is included in the result set. However, when I recreate my EF6 .edmx data model, MyColumn is not reflected in the EF vwMyView object or in MySproc_Result.
Is there a special trick I need to use so my new column MyColumn is reflected in a recreated EF6 .edmx? Is this a quirk that you encounter sometimes?
Happens sometimes, especially when the output is generated runtime as a polymorphic behavior (different result sets based on conditions)
In EF, for a result set, you may need to explictly create an output mapping. No need to delete the whole EDMX, just select the mapping (MySproc_Result) and update the result set to include the new column.
Are you creating the view inside procedure and then returning its value?
Inside the view, check if the columns are added conditionally (eg, if type=Sales, include 4 columns, if type=Mgmt, include 5 columns). The EF will pick up the 1st matching case in this condition.
Best way is to update the mapping and have the new column set as nullable, just in case some conditional makes it unavailable.
I have one table that has many fields like ID,Username,Password,Lang,Date,..
When I create Entity Data modeling so all the table field is generated.
I want to generate only specific column like username and password only.
When you generate from database it will generate model for all the columns initially, but you have the freedom to delete the columns that you don't want manually from the generated database EDMX diagram.
Just select the field you don't want and hit 'DEL' key.
Make sure that the fields you delete are nullable ones and also not primary key for that table.
Using EntityFramework Reverse POCO Generator v2.26.0 and I cannot find where to change the .tt to stop the column rename when generating the POCOs. I suspect it is in UpdateColumn, which I've updated to just the single line:
UpdateColumn = (Column column, Table table) => column;
But still the columns get renamed from e.g. "Batch_ID" to "BatchId".
Without stopping the column rename, I'm getting the error:
The data reader is incompatible with the specified 'DocumentExport.DataAccess.Databases.Batches.Batch'. A member of the type, 'BatchId', does not have a corresponding column in the data reader with the same name.
How does one stop column renaming during POCO generation?
In the database.tt,
UsePascalCase = false; // This will rename the generated C# tables & properties to use PascalCase. If false table & property names will be left alone.
While this accomplished suppressing column names, it also affected table names and possibly other things.
I am creating an Entity Framework 6 database using the Empty EF Designer Model.
My issue can be duplicated as follows. I have two tables and a many to many association between them.
When I generate the database, it creates a third mapping table, which I expect. However, the names of the columns are unexpected.
CREATE TABLE [dbo].[WordWordType] (
[Words_WordId] int NOT NULL,
[WordTypes_WordTypeId] int NOT NULL
);
Notice that the mapping table has column names that are somewhat redundant and long. The column names include the table name, which is redundant, followed by an underscore, followed by the Key column name.
WordWordType
- Words_WordId
- WordTypes_WordTypeId
I am hoping EF 6 has a way to create the mapping table with column names that don't include the table name and underscore. I need the table to look as follows.
WordWordType
- WordId
- WordTypeId
At the moment you already have the table name in your primary key column name, which is - in my opinion - a redundant information.
If your name your primary key columns simply Id, then EF will automatically name your columns Word_Id and WordType_Id.
In Code First you can use fluent API
modelBuilder.Entity<Word>()
.HasMany(w => w.WordTypes)
.WithMany(wt => wt.Words)
.Map(x =>
{
x.ToTable("WordWordType");
x.MapLeftKey("WordId");
x.MapRightKey("WordTypeId");
});
but since you are using model first I think the only way is to change T4 file which is responsible for generating SQL script. You can find it in Database Script Generation section in your model properties.
Update
see this for debugging T4.
the SSDLToSQL10.tt contains two main section for this, one is Creating all tables on line 150 and another one is Creating all FOREIGN KEY constraints on line 196, they enumerate on Store.GetAllEntitySets() and Store.GetAllAssociationSets() and creating tables and foreign keys in database, debug the T4 file and see where your mapping table is created, I suspect it is in Store.GetAllEntitySets(), then change the generation the way you want.
I am using a SQLite database. Since drop/rename/reorder columns are not supported in SQLite (using alter table commands), I am writing customized methods for the following tasks: (taking backup of existing table, then creating a new table with matching requirements and so on..) which is described in several other threads.
The following are the DB operations:
DROP columns
RENAME (columns and datatype)
ADD columns
REORDER columns.
I am wondering in what order should these operations be done? My confusion is mainly around whether drop should come before rename columns or the other way?
Also I need some pointers on how to rename columns including datatype and moving data around?
Any thoughts?
First, create a backup of your database in case something goes wrong during the following instructions.
The order of operations should be CREATE newtable, INSERT INTO newtable, DROP oldtable.
Create a new table with the correct column names and datatypes for each column. Then just do something like this:
INSERT INTO new_table(columns, ...)
(SELECT columns, ...
FROM old_table)
You may need to perform casts on different datatypes if the new datatype isn't directly compatible with the old datatype.
You will need to make sure that the columns you select from your old table are in the same order as defined by the columns in your new table in your INSERT INTO statement.
After data is inserted into your new table, you can then verify that all data has been inserted correctly. Be sure to update any foreign key references in your other tables to avoid any issues with foreign key constraints.
Then you can drop the old table and rename the new table to the name of your old table:
DROP old_table;
ALTER TABLE new_table_name RENAME TO old_table_name;