Create custom entity on the fly from a SQL query - entity-framework

I'd like to create an entity from a custom SQL query. Is that possible with Entity Framework 4?

I remember now. ExecuteStoryQuery will do just that.
See Directly Executing Store Commands

Related

Entity Framework Core Get related data without nesting

Is there a way using Entity Framework Core to get nested data without having to iterate through it? like a simple SQL select with joins i would like to show all data on the same table
Sounds like you know the SQL you need to get the data you want. So I suggest you create a database view and then map your ef entity to that. As long as you are just reading ef will treat the view just like it would any other table.
I think youre looking for .Include() and .ThenInclude() methods that allow you to load related data in a eager way.
More on that topic on the official entity framework documentation at MSDN

entity framework uses Datareader?

I have a simple question: if I have a simple EF query and I execute it, than it works like follows:
it generates an sql query
it runs the query against the database
it fills the entity object list with data from a simple datareader?
Thanks.

How to create Indexed Views with entity framework code-first approach

I have a 3 tables which need to be joined for doing some queries. The tables are relatively read-only for specific duration of time. Only if there is need, we need to write them.
I want to avoid join on demand for these tables. So I was planning to use Indexed views. However, I didn't find a way to define a view from entity framework (EF 6.1).
Can someone please guide on this?
Regards,
Amit Rangari
There is no direct method for creating views from EF 6.
You need to write raw SQL to create the view, then execute it from a dbMigration. For details see: https://msdn.microsoft.com/en-us/magazine/dn519921.aspx
You need to create the view via standard SQL statement (migration or the connection of the context). The view should have a key. After that you can map the view with EF like it was a table.

dynamic creating class in C#

I want to know if there is a way to generate the class dynamically at runtime. I want to use it in Entity framework code first.
Consider if I have 100 tables(or connect to unknown database) I will have to create model/POCO for each table in EF Code First, instead of this I want to generate the POCO class and all its properties at runtime based on the database connected.
Probably not.
Consider this... If the classes aren't defined before compilation, then how is any other code going to use them? If no other code is going to use them, why do you need them?
You can generate objects based on the table schema at design time. Doesn't Entity Framework in fact do this already?
I realize that I've linked to something that is "database first" instead of "code first" but, well, that's what you're asking:
I want to use it in Entity framework code first.
[...]
I will have to create model/POCO for each table in EF
You have a database, and you want to generate models based on the schema of that database. That's database-first.
You can use the EF Power Tools (beta 3) for Visual Studio 2010 or 2012 to reverse engineer a database to POCOs. After installation, right click on a project and select Reverse Engineer Code First under the new Entity Framework menu group.
The Power tools: http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d
Rowan Miller has a blog post about this, and some advanced uses: http://romiller.com/2012/05/09/customizing-reverse-engineer-code-first-in-the-ef-power-tools/

EF CodeFirst - Create index after database create

I'm migrating my project from database-first to code-first.
Entity Framework does some nice work creating my new database (that should mimic the old one).
I'm using a combination of data annotations and the fluent API to describe my tables.
My database has a few indexes and I would like Entity Framework to create them as well. It seems the old way to do this is to define your own Initializer and use custom T-SQL.
But now that we have EF Migrations it should be easier to do so.
I can't seem to figure out how to combine CreateDatabaseIfNotExists<> with an automatic migration to create the indexes. I've tried to use the MigrateDatabaseToLatestVersion<,> but it doesn't seem to perform the migration after the Database has been created.
What is the proper way to create indexes and constraints on database creation now that we have Entity Framework 4.3?
Don't use CreateDatabaseIfNotExists if you want to use migrations. Use MigrateDatabaseToLatestVersion from the beginning - it will create your database as well. Put your index creation code (calls to CreateIndex) into Up method of your migration class.
If you already have existing database and you want to use migrations you must first create initial migration.