-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
36 lines (32 loc) · 1.02 KB
/
Copy pathProgram.cs
File metadata and controls
36 lines (32 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Data.Entity;
using System.Linq;
namespace UdfCodeFirstSample
{
internal class Program
{
private static void Main(string[] args)
{
// add seed data
using (var db = new TownContext())
{
db.People.Add(new Person {Name = "Alfreds Futterkiste", BirthDate = new DateTime(1980, 1, 1)});
db.SaveChanges();
}
// invoke the user defined function in a LINQ to Entities query
using (var db = new TownContext())
{
var age = db.People.Select(p => new {p.Name, Age = Fun.GetAge(p.BirthDate)}).FirstOrDefault();
Console.WriteLine(age);
}
}
}
public class TownContext : DbContext
{
public DbSet<Person> People { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add(new GetAgeStoreFunctionInjectionConvention());
}
}
}