I have a query where I'm doing a Count on a specific join/column - If I run this code with the "Clicks" [NotMapped] attribute removed, all the values populate properly - but then inserts fail since "Clicks" is not a valid column name. When I mark the column as [NotMapped] then it's not populating from this statement. How can I use raw SQL and populate a [NotMapped] column?
Code:
var query = db.URLs.FromSqlRaw(
@"SELECT [u].[Key], [u].[Url], COUNT(c.Id) AS [Clicks]
FROM[URLs] AS[u]
LEFT JOIN[Clicks] AS[c] ON[u].[Key] = [c].[ShortUrlKey]
GROUP BY[u].[Key], [u].[Url]")
.AsQueryable<ShortURL>();
var urls = query.ToList();
Model (works for inserts, but doesn't populate Clicks property):
public class ShortURL
{
public string Key { get; set; }
public string Url { get; set; }
[NotMapped()]
public int Clicks { get; set; } // doesn't populate from raw query
}
Model (works for queries, but fails on inserts)
public class ShortURL
{
public string Key { get; set; }
public string Url { get; set; }
public int Clicks { get; set; } // not in DB
}