Mapowanie widoków bazy danych do kodu EF 5.0 Najpierw w / Migracje
Próbuję odwzorować widok SQL na encję w kodzie EF 5.0 Najpierw w / Migracje, aby wyświetlić podstawowe informacje na stronie bez konieczności wysyłania zapytań do wielu tabel dla tej informacji (trwa to obecnie ~ 20 sekund.NIEDOBRZE.). Słyszałem, że można to zrobić, ale nie udało mi się znaleźć ani znaleźć sposobu na prawidłowe wykonanie tego.
EDYCJA: Aby uzyskać bardziej dogłębne spojrzenie na moje rozwiązanie tego problemu, przeczytajten wpis na blogu w temacie.
Oto mój widok:
CREATE VIEW [dbo].[ClientStatistics]
AS
SELECT ROW_NUMBER() OVER (Order By c.ID) as Row, c.LegacyID, c.ID, c.ClientName, slc.AccountManager, slc.Network,
(SELECT MAX(CreatedDate) AS Expr1
FROM dbo.DataPeriods
WHERE (ClientID = c.ID)) AS LastDataReceived,
(SELECT MAX(ApprovedDate) AS Expr1
FROM dbo.DataPeriods AS DataPeriods_2
WHERE (ClientID = c.ID)) AS LastApproved,
(SELECT MAX(ReportProcessedDate) AS Expr1
FROM dbo.DataPeriods AS DataPeriods_1
WHERE (ClientID = c.ID)) AS LastReportProcesssed
FROM dbo.Clients AS c INNER JOIN
dbo.SLClients AS slc ON c.ID = slc.ClientID
Oto podmiot:
public class ClientStatisticsView
{
[Key]
public int Row { get; set; }
public int LegacyID { get; set; }
public int ClientID { get; set; }
public string ClientName { get; set; }
public string AccountManager { get; set; }
public string Network { get; set; }
public DateTime LastDataReceived { get; set; }
public DateTime LastApproved { get; set; }
public DateTime LastReportProcessed { get; set; }
}
I wreszcie moje mapowanieDbContext
:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Entity<ClientStatisticsView>().ToTable("ClientStatistics");
base.OnModelCreating(modelBuilder);
}
Wszystko to powoduje następujący błąd:
There is already an object named 'ClientStatistics' in the database.
Co ja robię źle? Czy jest jakiś sposób, aby to osiągnąć, czy raczej powinienem robić coś innego?