Working with SQL views in Entity Framework Core

后端 未结 6 2242
再見小時候
再見小時候 2020-11-30 03:07

For example, I have such model:

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public BlogImage BlogImage {         


        
6条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 03:30

    QueryTypes is the canonical answer as of EF Core 2.1, but there is another way I have used when migrating from a database first approach (the view is already created in the database):

    • define the model to match view columns (either match model class name to view name or use Table attribute. Ensure [Key] attribute is applied to at least one column, otherwise data fetch will fail
    • add DbSet in your context
    • add migration (Add-Migration)
    • remove or comment out code for creation/drop of the "table" to be created/dropped based on provided model
    • update database (Update-Database)

提交回复
热议问题