EF Core 3.1 Template - Determine if EntityClass is SQL view or table

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
mrmiagi0101
Posts: 23
Joined: Tue 27 Feb 2018 13:43

EF Core 3.1 Template - Determine if EntityClass is SQL view or table

Post by mrmiagi0101 » Wed 10 Jun 2020 10:24

Hello,

is there any way to determine if a EntityClass is mapped to a Sql View or Table?
Because currently both types get configured as .ToView() in generated EF Core configuration (QueryType = true).

I was able to fix this currently with doing by own sql select to DB in the T4 template to check if sql object is View or Table.
I also set the color of Views to a different color as the color for tables.

But I would be nice to avoid this additional sql select in template.
So a way to determine if View or Table would be nice.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: EF Core 3.1 Template - Determine if EntityClass is SQL view or table

Post by Shalex » Sat 04 Jul 2020 13:36

is there any way to determine if a EntityClass is mapped to a Sql View or Table?
1. We will investigate the possibility to distinguish views and tables in the model with some attribute automatically. Could you describe your use case?
a) why do you need an attribute in the model to determine if an EntityClass is mapped to a view or table?
b) how are you going to use this attribute in code generation?
Because currently both types get configured as .ToView() in generated EF Core configuration (QueryType = true).
2. QueryType=True should be set for tables without PK and views.
I also set the color of Views to a different color as the color for tables.
3. We will consider adding the option in Create Model Wizard and Update From Database Wizard for setting different colors for tables and views.

mrmiagi0101
Posts: 23
Joined: Tue 27 Feb 2018 13:43

Re: EF Core 3.1 Template - Determine if EntityClass is SQL view or table

Post by mrmiagi0101 » Mon 06 Jul 2020 19:59

a) + b) I only need this for replacing your logic for creating the entity configuration. Because you always use ToView if QueryType = true. But this is wrong, If you have a table, you should really use ToTable with HasNoKey instead. Because we use Ef Cores buildin "DbContext.Database.EnsureCreated()" methods for testing our own framework based on EF Core. So EF Core creates the tables from the applied entity configurations of a DBContext in our integrationTests. But if there is ToView used, it ignores that object and is not generating the tables.

See this on Ef Core documentation:
"Note
ToView assumes that the object already exists in the database and it won't be created by migrations."

https://docs.microsoft.com/en-us/ef/cor ... nnotations

So I wanted to change this in the Template, but I realized there is no way to find out what we really have (Table or View) in template.
And the second thing was to apply different default colors as I mentioned.

I have another new reqiurement, but It look like it is also missing in the tamplate. I would need to get the complete sql definition of a stored procedure of function. I think you already fetch this from the sql object, but you do not pass this info to template. Or is there a way to see the sql of it? My goal is to get the info if a sql parameter has a default value or not.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: EF Core 3.1 Template - Determine if EntityClass is SQL view or table

Post by Shalex » Fri 10 Jul 2020 18:07

Because you always use ToView if QueryType = true. But this is wrong, If you have a table, you should really use ToTable with HasNoKey instead.
4. You are right. We will fix the current behavior and notify you.
I would need to get the complete sql definition of a stored procedure of function. I think you already fetch this from the sql object, but you do not pass this info to template. Or is there a way to see the sql of it? My goal is to get the info if a sql parameter has a default value or not.
5. Default values of the stored procedure's parameters are currently not available via the interface of Entity Developer. We will investigate the question and let you know.

Post Reply