fork download
  1. // Итого, сущности наследуем от Entity.
  2. // Маппинги от BaseMap<TEntity> или EntityTypeConfiguration<TEntity>.
  3. // В контексте тот же метод переопрделяем, что и в DemoAppDbContext.
  4.  
  5. // Достоинства - EF CF
  6. // Маппинги в ондй сборке отдельно
  7. // Нормальное интстанцирование маппингов черех один метод.
  8.  
  9. namespace DemoApp.Extensions
  10. {
  11. public static class TypeExtensions
  12. {
  13. public static bool IsAssignableFromGeneric(this Type t, Type type)
  14. {
  15. if (t == null)
  16. {
  17. throw new ArgumentNullException("t");
  18. }
  19.  
  20. while (t.BaseType != null)
  21. {
  22. if (t.BaseType.IsGenericType && !t.BaseType.ContainsGenericParameters &&
  23. t.BaseType.GetGenericTypeDefinition() == type)
  24. {
  25. return true;
  26. }
  27.  
  28. t = t.BaseType;
  29. }
  30.  
  31. return false;
  32. }
  33. }
  34. }
  35.  
  36. namespace DemoApp.Data.Model.Contracts
  37. {
  38. // Базовый интерфейс для всех сущностей, хранимых в базе.
  39. public interface IEntity
  40. {
  41. Guid Id { get; set; }
  42. bool IsTransient { get; }
  43. }
  44. }
  45.  
  46. namespace DemoApp.Data.Model.Entities
  47. {
  48. // Базовый класс для всех сущностей, хранимых в базе.
  49. public abstract class Entity : IEntity
  50. {
  51. // TODO: Поменять на int?
  52. public Guid Id { get; set; }
  53.  
  54. public bool IsTransient
  55. {
  56. get { return this.Id == Guid.Empty; }
  57. }
  58. }
  59. }
  60.  
  61. namespace DemoApp.Data.EntityFramework.Mappings
  62. {
  63. // Базовый класс для большинства маппингов, для остальных EntityTypeConfiguration<TEntity>
  64. public abstract class BaseMap<TEntity> : EntityTypeConfiguration<TEntity> where TEntity : class, IEntity
  65. {
  66. protected BaseMap()
  67. {
  68. this.HasKey(t => t.Id);
  69.  
  70. this.Property(t => t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  71. }
  72. }
  73. }
  74.  
  75. namespace DemoApp.Data.EntityFramework.DataContexts
  76. {
  77. public class DemoAppDbContext : DbContext
  78. {
  79. #region Constructor
  80.  
  81. public OlympiatoppenDbContext([NotNull] string nameOrConnectionString)
  82. : base(nameOrConnectionString, validationMessages)
  83. {
  84. }
  85.  
  86. #endregion
  87.  
  88. #region DbSets
  89.  
  90.  
  91. #endregion
  92.  
  93. #region Overrides of DbContext
  94.  
  95. protected override void OnModelCreating([NotNull] DbModelBuilder modelBuilder)
  96. {
  97. var modelConfigurationTypes = typeof (BaseMap<>).Assembly.GetTypes()
  98. .Where(type => !type.IsAbstract && type.IsAssignableFromGeneric(typeof (EntityTypeConfiguration<>)));
  99.  
  100. foreach (dynamic configuration in modelConfigurationTypes.Select(Activator.CreateInstance))
  101. {
  102. modelBuilder.Configurations.Add(configuration);
  103. }
  104.  
  105. base.OnModelCreating(modelBuilder);
  106. }
  107. }
  108. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(75,16): error CS1520: Class, struct, or interface method must have a return type
prog.cs(102,1): error CS1038: #endregion directive expected
Compilation failed: 2 error(s), 0 warnings
stdout
Standard output is empty