forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManyToOne.cs
92 lines (79 loc) · 2.6 KB
/
ManyToOne.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
82
83
84
85
86
87
88
89
90
91
92
using System.Collections.Generic;
using NHibernate.Type;
using System;
using System.Linq;
namespace NHibernate.Mapping
{
/// <summary> A many-to-one association mapping</summary>
[Serializable]
public class ManyToOne : ToOne
{
/// <summary>
///
/// </summary>
/// <param name="table"></param>
public ManyToOne(Table table) : base(table)
{
}
/// <summary></summary>
public override void CreateForeignKey()
{
// the case of a foreign key to something other than the pk is handled in createPropertyRefConstraints
if (ReferencedPropertyName == null && !HasFormula && !IsIgnoreNotFound)
{
CreateForeignKeyOfEntity(((EntityType)Type).GetAssociatedEntityName());
}
}
private bool isIgnoreNotFound = false;
private bool isLogicalOneToOne;
public bool IsIgnoreNotFound
{
get { return isIgnoreNotFound; }
set { isIgnoreNotFound = value; }
}
public bool IsLogicalOneToOne
{
get { return isLogicalOneToOne; }
set { isLogicalOneToOne = value; }
}
public string PropertyName { get; set; }
private IType type;
public override IType Type
{
get
{
if (type == null)
{
type =
TypeFactory.ManyToOne(ReferencedEntityName, ReferencedPropertyName, IsLazy, UnwrapProxy, IsIgnoreNotFound, isLogicalOneToOne, PropertyName);
}
return type;
}
}
public void CreatePropertyRefConstraints(IDictionary<string, PersistentClass> persistentClasses)
{
if (!string.IsNullOrEmpty(referencedPropertyName))
{
if (string.IsNullOrEmpty(ReferencedEntityName))
throw new MappingException(
string.Format("ReferencedEntityName not specified for property '{0}' on entity {1}", ReferencedPropertyName, this));
PersistentClass pc;
persistentClasses.TryGetValue(ReferencedEntityName, out pc);
if (pc == null)
throw new MappingException(string.Format("Could not find referenced entity '{0}' on {1}", ReferencedEntityName, this));
Property property = pc.GetReferencedProperty(ReferencedPropertyName);
if (property == null)
throw new MappingException("Could not find property " + ReferencedPropertyName + " on " + ReferencedEntityName);
if (!HasFormula && !"none".Equals(ForeignKeyName, StringComparison.OrdinalIgnoreCase))
{
IEnumerable<Column> ce = property.ColumnIterator.OfType<Column>();
// NH : Ensure that related columns have same length
ForeignKey.AlignColumns(ConstraintColumns, ce);
ForeignKey fk =
Table.CreateForeignKey(ForeignKeyName, ConstraintColumns, ((EntityType)Type).GetAssociatedEntityName(), ce);
fk.CascadeDeleteEnabled = IsCascadeDeleteEnabled;
}
}
}
}
}