1

I know how to map table from tutorial

public DbSet<AccountEntity> Account { get; set; }
//..... OnModelCreating
modelBuilder.Entity<AccountEntity>().ToTable("Account");

Let's say there is an view in database named V_Account, how can I map to this view and use it as _context.V_Account?

4
  • i can't post an answer today but check this may help Commented Apr 6, 2017 at 17:49
  • As long as the model matches the view, try modelBuilder.Entity<AccountEntity>().ToTable("V_Account").HasKey(a => a.AccountId); Commented Apr 12, 2017 at 5:45
  • @ModarNa That article refers to "Mapping Views" which is an internal detail of EF, but the OP is asking about VIEW objects: despite both being called "View", they're different things. Commented Aug 8, 2022 at 0:14
  • @MarkG To my knowledge, you cannot use ToTable() to map a VIEW to an existing Entity Class that already has a TABLE configured. Commented Aug 8, 2022 at 0:14

1 Answer 1

1

As far as I am concerned it's not possible yet to map to a view. See the "Critical O/RM features" on the EF Core Roadmap Docs on GitHub

Critical O/RM features

The things we think we need before we say EF Core is the recommended version of EF. Until we implement these features EF Core will be a valid option for many applications, especially on platforms such as UWP and .NET Core where EF6.x does not work, but for many applications the lack of these features will make EF6.x a better option.

Modelling

  • Complex/value types are types that do not have a primary key and are used to represent a set of properties on an entity type.
  • Stored procedure mapping allows EF to use stored procedures to persist changes to the database (FromSql already provides good support for using a stored procedure to query).
  • View mapping allows EF to map to database views.

See the last point, which means that it's still on the roadmap. Since it's not yet in the Roadmap for EF Core 2.0 it's to be assumed that it won't come until a later version.

If you need such features you should fall back to using EF 6.x (but then unable to target .NET Core, just .NET Framework 4.x)

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

Comments

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.