Page 1 of 1

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

Posted: Wed 10 Jun 2020 10:24
by mrmiagi0101
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.

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

Posted: Sat 04 Jul 2020 13:36
by Shalex
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.

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

Posted: Mon 06 Jul 2020 19:59
by mrmiagi0101
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.

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

Posted: Fri 10 Jul 2020 18:07
by Shalex
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.