0

In Entity Framework Core, how can I access the Entity Type data (the entity names, regular properties, navigation properties, etc.) when using code-first? When using database-first, I can use Scaffolding or Reverse Engineering and from EF Core 7 onward, I can even use Custom Reverse Engineering Templates. I'd like to be able to access the same Entity Type information when using code-first whether that be in T4 or in plain C#. Based on the default T4 template in EF Core 7, the information I am after is in the Model parameter (of type Microsoft.EntityFrameworkCore.Metadata.IModel) that gets set as a parameter:

<#@ parameter name="Model" type="Microsoft.EntityFrameworkCore.Metadata.IModel" #>

I'm essentially asking a variation of this EF 6 question about DbContext Generation T4 Templates With Code-First but for EF Core.

1 Answer 1

1

You can access it on an instance of DbContext. There is property Model available.

Related information

DbContext.Model Property

Sign up to request clarification or add additional context in comments.

2 Comments

@JohnCummings, DbSet property is syntax sugar, EF do not use in runtime these properties for functioning. Referencing Microsoft.EntityFrameworkCore.Design is tricky, check this my answer
Simple enough - thank you. Now I'm fighting getting access to the extensions methods such as GetDbSetName provided by ScaffoldingModelExtensions Visual Studio and the compiler can't seem to find them even with a using Microsoft.EntityFrameworkCore and using Microsoft.EntityFrameworkCore.Design and those packages installed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.