Regenerating entity objects via EDMGEN in an MSBUILD project - entity-framework

I have my build executing edmgen successfully however it's generating entity objects for all of my database tables. What I'd like to have it do is only generate for select tables like it currently does in the project locally.
I'm using full generation and passing in a connection string.

There is no possibility to work with a subset of objects in EDMGen. You can try creating a new schema containing only the necessary tables, and run EDMGen for this schema only.

Alternatively, you can create your own version of EdmGen by using the EdmGen2 code base.
In the ModelGen method you can define EntityStoreSchemaFilterEntry filters to select which tables you want.

Related

Development process for Code First Entity Framework and SQL Server Data Tools Database Projects

I have been using Database First Entity Framework (EDMX) and SQL Server Data Tools Database Projects in combination very successfully - change the schema in the database and 'Update Model from Database' to get them into the EDMX. I see though that Entity Framework 7 will be dropping the EDMX format and I am looking for a new process that will allow me to use Code First in Combination with Database Projects.
Lots of my existing development and deployment processes rely on having a database project that contains the schema. This goes in source control is deployed along with the code and is used to update the production database complete with data migration using pre and post deployment scripts. I would be reluctant to drop it.
I would be keen to split one big EDMX into many smaller models as part of this work. This will mean multiple Code First models referencing the same database.
Assuming that I have an existing database and a database project to go with it - I am thinking that I would start by using the following wizard to create an initial set of entity and context classes - I would do this for each of the models.
Add | New Item... | Visual C# Items | Data | ADO.NET Entity Data Model | Code first from database
My problem is - where do I go from there? How do I handle schema changes? As long as I can get the database schema updated, I can use a schema compare operation to get the changes into the project.
These are the options that I am considering.
Make changes in the database and use the wizard from above to regenerate. I guess that I would need to keep any modifications to the entity and/or context classes in partial classes so that they do not get overwritten. Automating this with a list of tables etc to include would be handy. Powershell or T4 Templates maybe? SqlSharpener (suggested by Keith in comments) looks like it might help here. I would also look at disabling all but the checks for database existence and schema compatibility here, as suggested by Steve Green in the comments.
Make changes in code and use migrations to get these changes applied to the database. From what I understand, not having models map cleanly to database schemas (mine don't) might pose problems. I also see some complaints on the net that migrations do not cover all database object types - this was also my experience when I played around with Code First a while back - unique constraints I think were not covered. Has this improved in Entity Framework 7?
Make changes in the database and then use migrations as a kind of comparison between code and the database. See what the differences are and adjust the code to suit. Keep going until there are no differences.
Make changes manually in both code and the database. Obviously, this is not very appealing.
Which of these would be best? Is there anything that I would need to know before trying to implement it? Are there any other, better options?
So the path that we ended up taking was to create some T4 templates that generate both a DbContext and our entities. We provide the entity T4 a list of tables from which to generate entities and have a syntax to indicate that the entity based on one table should inherit from the entity based on another. Custom code goes in partial classes. So our solution looks most like my option 1 from above.
Also, we started out generating fluent configuration in OnModelCreating in the DbContext but have swapped to using attributes on the Entities (where attributes exist - HasPrecision was one that we had to use fluent configuration for). We found that it is more concise and easier to locate the configuration for a property when it is right there decorating that property.

How to update/regenerate CRUD code using OIL package in FuelPHP

I am new to FuelPHP ... I have generated curd code using the following command php oil generate scaffold/crud monkey name:string still_here:bool
it generate migration files, views, controller, and model.. than I use the following command to create database tables php oil refine migrate everything is fine...
but what i need now is to update the table means i want to add more columns to the monkey table in database.... how and what is the reliable way to do this?? so that it will auto update the files as well
There is no built in way of doing this. The only thing you can do is generating migrations or manually creating them and updating your models.
See how to generate migrations: http://fuelphp.com/docs/packages/oil/generate.html#migrations

Generating SQL Server Views from EDMX using T4 templates

I'm working with a legacy database that I can't easily create an entity model over because it uses extension tables with what is effectively composite keys and EF only supports single column keys for mapping one entity to multiple tables.
So, what I've decided to do is create updatable views (with INSTEAD OF triggers to handle CRUD operations) over the top of the legacy tables (which cannot be touched) and then have my entity model (either using EF or DevExpress XPO) built on top of the database views. This will also allow me to do stuff like easily add sub-queries in the select clause to retrieve child counts on parent records when retrieving a list of parent records in a single query.
However, I don't particularly want to manually write the SQL for all the views and triggers so I thought I'd use data model defined in the .EDMX file and t4 templates to help me generate the bulk of the T-SQL needed to create the views and the triggers. I thought there would be some template that I could use as the basis for doing this, but seems that's not so easy to find.
Can someone please suggest a t4 template that I could use as the basis where mappings are being retrieved from the .EDMX. Alternatively can anyone advise how to use the StorageMappingItemCollection to retrieve the mapping information from the EDMX file. I know a few people have said that apparently you can't use it or that they just use Linq to Xml, but I would have thought it should certainly be possible to use the StorageMappingItemCollection class as a strongly typed class to access this data.
Any examples of how I could use StorageMappingItemCollection to access mapping info would be very helpful. Thanks.
See http://brewdawg.github.io/Tiraggo.Edmx/ you can install it via NuGet within Visual Studio and it serves up all of the metadata from your EDMX files that Microsoft hides from you, very simple, works great.

Is there any way to use Linq To Entites without `.edmx` file?

I created Model1.edmx file and accordingly Model1.Designer.cs was also created.
I decided to copy all code from Model1.Designer.cs to dal.cs file and deleted Model1.edmx + Model1.Designer.cs files.
When I try to connect I get error that mapping is failed and no SSDL,CSDL are found.
Is there any way to use Linq To Entites without .edmx file, but just using the code in Model1.Designer.cs?
If you define your mapping in EDMX there is no way to use it without EDMX. EDMX is necessary because the build process will decompose the EDMX files into multiple resources specifying mapping between classes and database. These resources are used at runtime.
If you don't want to have EDMX file you can't use it at all and instead you can try code first approach (more tutorials are available on that page) in Entity Framework 4.1. You can also use helper EF Power Tools to generate code mapping from existing database for you (it can be good to start learning how to map tables from code).

EF model mapping multiple databases

I have a model in my project that maps to a LOT of views in my database, but I need to map to a view in another database.
How can I do this? Do I have to create another model? I don't want to, but I will if I have to.
The same model can't get data from the two different DBs. The easiest way would be to create a view in the same database that calls and returns data from the other database i.e. the abstraction view that internally calls external DB view.
If your database supports synonyms, you could setup a synonym to the other database, and merge the edmx definition in with your 1st database's definition. I wrote how to do it here
Basically you end up with two edmx files, and a script that merges the two into a working edmx file. Synonyms are used to reference one database from the other without needing the full database path.
If you use code first approach in Entity Framework, here is how to map EF entity to the table from other database:
SQL Script that needs to be run in your database to create synonym for the table from other database:
CREATE SYNONYM OtherDatabaseTableSynonym FOR otherdatabase.dbo.otherdatabasetable
Entity Framework Mapping in (Fluent API):
modelBuilder.Entity<OtherDatabaseTableEntity>().ToTable("OtherDatabaseTableSynonym").HasKey(x => x.id);