-
Notifications
You must be signed in to change notification settings - Fork 475
/
Copy pathEdmModelBuilder.cs
81 lines (63 loc) · 2.17 KB
/
EdmModelBuilder.cs
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Microsoft.AspNet.OData.Builder;
using Microsoft.OData.Edm;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace AspNetCore3xEndpointSample.Web.Models
{
public static class EdmModelBuilder
{
private static IEdmModel _edmModel;
public static IEdmModel GetEdmModel()
{
if (_edmModel == null)
{
var builder = new ODataConventionModelBuilder();
var customers = builder.EntitySet<Customer>("Customers");
customers.Binding.HasManyPath(c => c.CustomerReferrals, true).HasRequiredBinding(r => r.ReferredCustomer, "Customers");
// builder.EntitySet<Order>("Orders");
_edmModel = builder.GetEdmModel();
}
return _edmModel;
}
}
public class Customer
{
[Key]
public int ID { get; set; }
[Contained]
public virtual ICollection<CustomerReferral> CustomerReferrals { get; set; }
[Contained]
public virtual ICollection<CustomerPhone> Phones { get; set; }
}
public class CustomerReferral
{
[Key]
public int ID { get; set; }
public int CustomerID { get; set; }
public int ReferredCustomerID { get; set; }
[Required]
[ForeignKey(nameof(CustomerID))]
public virtual Customer Customer { get; set; }
[Required]
[ForeignKey(nameof(ReferredCustomerID))]
public virtual Customer ReferredCustomer { get; set; }
}
public class CustomerPhone
{
[Key]
public int ID { get; set; }
[Editable(false)]
public int CustomerID { get; set; }
[Contained]
public virtual CustomerPhoneNumberFormatted Formatted { get; set; }
}
public class CustomerPhoneNumberFormatted
{
[Key]
public int CustomerPhoneNumberID { get; set; }
public string FormattedNumber { get; set; }
}
}