简体   繁体   中英

EF Core: How to query a SQL View

I have a SQL View in my DB called 'dbo.Viewtest'. This view combines the data of 2 other tables, that have identical column types. The view also adds a column to show from which table the data originated.

This is what the SQL View looks like in SQL Server Object Explorer:

dbo.ViewTest

Id | Type | Content | Location

1 | h1 | Welcome!| Home

2 | p | Log in | Home

3 | h2 | guide | Home

1 | h1 | info | Events

2 | p | follow | Events

This view is created from 2 tables, one of which is called 'HomeContent' and the other 'EventsContent' (hence the location column). The view was created, using the following code:

protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql("CREATE VIEW ViewTest AS " +
                "SELECT Id, Type, Content, 'Home' location FROM HomeContent UNION ALL " +
                "SELECT Id, Type, Content, 'Evenementen' FROM EvenementenContent");

        }

I then added a Model Class as follows:

  [Table("ViewTest")]
    public class TestViewContent
    {
        [Key]
        public int Id { get; set; }
        public string Type { get; set; }
        public string Content { get; set; }
        public string Location { get; set; }
    }

And finally, i've added it to my DbContext:

 public DbSet<TestViewContent> TestViewContent { get; set; }

Now, here is where things start breaking. When i try to display this SQL View, using EF Core, i get the following broken output:

Id | Type | Content | Location

1 | h1 | Welcome!| Home

2 | p | Log in | Home

3 | h2 | guide | Home

1 | h1 | Welcome | Home

2 | p | log in | Home

As you can see, for whatever reason when i try to display the SQL View, the data 'loops-around' when data from the 'Events' table is supposed to show. So for whatever reason, it only displays data from the home table.

The code that i use for displaying the SQL View is:

 private readonly DbApplicationContext_context;

        public TestViewContentController(DbApplicationContext_context)
        {
            _context = context;
        }

   public async Task<IActionResult> Index()
        {
            return View(await _context.TestViewContent.ToListAsync());
        }

Is there anyone who could help me out here? In case it isn't possible to display SQL-Views, does anyone know of a better way to combine multiple tables and add a 'Location' column to trace the table from where the data originated? Any help would be greatly appreciated!

This happens because you put [Key] on the Id property, but your VIEW produces duplicate values in the Id column when combining the two tables. EF Core then probably goes "Ah there is Id =1 again, I have that row already, so I will reuse it".

There are three possible fixes:

(1) Define a separate column for the VIEW that is guaranteed to generate unique values , and then use that column in EF Core as the [Key] .

or

(2) Keep the VIEW as it is, then in EF Core define a composite primary key . Note that EF Core does not support creating a composite key using the [Key] attribute, you need to use the Fluent API HasKey() function , in this case for the Location and Id properties.

or

(3) Leave out the [Key] altogether, you may not actually need it, as described in the docs here: Keyless Entity Types .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM