Guest User

Untitled

a guest
Jan 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. could not initialize a collection: [RealEstateDirectory.Domain.Entities.Dictionaries.District.Streets#65539][SQL: SELECT streets0_."District" as column1_1_, streets0_."Id" as column1_0_0_, streets0_."Name" as column2_0_0_, streets0_."District" as column1_17_0_ FROM "Street" streets0_ WHERE streets0_."District"=?]
  2.  
  3. The value "RealEstateDirectory.Domain.Entities.Dictionaries.District" is not of type "RealEstateDirectory.Domain.Entities.Dictionaries.Street" and cannot be used in this generic collection.
  4.  
  5. public abstract class Entity<TId>
  6. {
  7. public virtual TId Id { get; /*protected*/ set; }
  8.  
  9. public override bool Equals(object obj)
  10. {
  11. return Equals(obj as Entity<TId>);
  12. }
  13.  
  14. private static bool IsTransient(Entity<TId> obj)
  15. {
  16. return obj as object != null &&
  17. Equals(obj.Id, default(TId));
  18. }
  19.  
  20. protected virtual Type GetUnproxiedType()
  21. {
  22. return GetType();
  23. }
  24.  
  25. protected virtual bool Equals(Entity<TId> other)
  26. {
  27. if (other as object == null)
  28. return false;
  29. if (ReferenceEquals(this, other))
  30. return true;
  31. if (!IsTransient(this) &&
  32. !IsTransient(other) &&
  33. Equals(Id, other.Id))
  34. {
  35. var otherType = other.GetUnproxiedType();
  36. var thisType = GetUnproxiedType();
  37.  
  38. return thisType.IsAssignableFrom(otherType) ||
  39. otherType.IsAssignableFrom(thisType);
  40. }
  41.  
  42. return false;
  43. }
  44.  
  45. public override int GetHashCode()
  46. {
  47. if (Equals(Id, default(TId)))
  48. return base.GetHashCode();
  49.  
  50. return Id.GetHashCode();
  51. }
  52.  
  53. public static bool operator ==(Entity<TId> entity1, Entity<TId> entity2)
  54. {
  55. if (entity1 as object == null)
  56. {
  57. return (entity2 as object == null);
  58. }
  59. else
  60. {
  61. return entity1.Equals(entity2);
  62. }
  63. }
  64.  
  65. public static bool operator !=(Entity<TId> object1, Entity<TId> object2)
  66. {
  67. return !(object1 == object2);
  68. }
  69.  
  70. public virtual T As<T>() where T : class
  71. {
  72. return this as T;
  73. }
  74.  
  75. public virtual bool Is<T>() where T : class
  76. {
  77. return this is T;
  78. }
  79. }
  80.  
  81. public abstract class BaseDictionary : Entity<int>
  82. {
  83. #region Свойства
  84.  
  85. public virtual string Name { get; set; }
  86.  
  87. #endregion
  88.  
  89. #region Конструкторы
  90.  
  91. protected BaseDictionary()
  92. {
  93. }
  94.  
  95. public BaseDictionary(string name)
  96. {
  97. Name = name;
  98. }
  99.  
  100. #endregion
  101. }
  102.  
  103. public class District : BaseDictionary
  104. {
  105. /// <summary>
  106. /// Streets in district
  107. /// </summary>
  108. public virtual IList<Street> Streets { get; set; }
  109.  
  110. protected District()
  111. {
  112. }
  113.  
  114. public District(string name)
  115. : base(name)
  116. {
  117. }
  118. }
  119.  
  120. public class Street : BaseDictionary
  121. {
  122. public virtual District District { get; set; }
  123.  
  124. protected Street()
  125. {
  126. }
  127.  
  128. public Street(string name, District district)
  129. : base(name)
  130. {
  131. District = district;
  132. }
  133. }
  134.  
  135. public class BaseDictionaryMap : ClassMapping<BaseDictionary>
  136. {
  137. public BaseDictionaryMap()
  138. {
  139. Id(x => x.Id, m => m.Generator(Generators.HighLow));
  140. Property(x => x.Name, m=>
  141. {
  142. m.NotNullable(true);
  143. m.Length(2048);
  144. m.Unique(true);
  145. });
  146. }
  147. }
  148.  
  149. public class StreetMap : UnionSubclassMapping<Street>
  150. {
  151. public StreetMap()
  152. {
  153.  
  154.  
  155. ManyToOne(x => x.District, r =>
  156. {
  157. r.Cascade(Cascade.All | Cascade.None | Cascade.Persist | Cascade.Remove);
  158. r.Update(true);
  159. r.Insert(true);
  160. r.NotNullable(true);
  161. //r.Class(typeof(District));
  162. });
  163. }
  164. }
  165.  
  166. public class DistrictMap : UnionSubclassMapping<District>
  167. {
  168. public DistrictMap()
  169. {
  170.  
  171. Bag(x => x.Streets, c =>
  172. {
  173. //c.Inverse(true);
  174. c.Lazy(CollectionLazy.NoLazy);
  175. },
  176. r =>
  177. {
  178. r.OneToMany(m =>
  179. {
  180. m.Class(typeof (Street));
  181.  
  182. }
  183. );
  184.  
  185. }
  186. );
  187.  
  188.  
  189. }
  190. }
  191.  
  192. public class RepositoryWithTypedIdBase<TId> : IRepositoryWithTypedId<TId>
  193. {
  194. public IEnumerable<T> GetAll<T>()
  195. {
  196. return CurrentSession.Query<T>().ToList();
  197. }
  198. }
  199.  
  200. var districtEntities = districtService.GetAll();
Add Comment
Please, Sign In to add comment