Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Generic Repository :</strong></p> <pre><code>public class EFRepository&lt;T&gt; where T : class { private DbContext _context; public EFRepository(DbContext context) { this._context = context; } public void Insert(T entity) { this._context.Entry&lt;T&gt;(entity).State = EntityState.Added; } public bool Commit() { return this._context.SaveChanges() &gt; 0; } } </code></pre> <p><strong>Domain Models :</strong></p> <pre><code>public class BaseEntity { public string BaseEntityProp1 { get; set; } public string BaseEntityProp2 { get; set; } } public class Entity1 : BaseEntity { public int Entity1ID { get; set; } } public class Entity2 : BaseEntity { public int Entity2ID { get; set; } } </code></pre> <p><strong>Table Per Concrete (TPC) Mappings :</strong></p> <pre><code>public class Entity1Map : EntityTypeConfiguration&lt;Entity1&gt; { public Entity1Map() { this.HasKey(e =&gt; e.Entity1ID); this.Property(e =&gt; e.Entity1ID) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); this.Map(m =&gt; { m.ToTable("Entity1"); m.MapInheritedProperties(); }); } } public class Entity2Map : EntityTypeConfiguration&lt;Entity2&gt; { public Entity2Map() { this.HasKey(e =&gt; e.Entity2ID); this.Property(e=&gt; e.Entity2ID) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); this.Map(m =&gt; { m.ToTable("Entity2"); m.MapInheritedProperties(); }); } } </code></pre> <p><strong>Context :</strong></p> <pre><code>public class MyContext : DbContext { public MyContext() { } public DbSet&lt;Entity1&gt; Entities1{ get; set; } public DbSet&lt;Entity2&gt; Entities2 { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new Entity1Map()); modelBuilder.Configurations.Add(new Entity2Map()); base.OnModelCreating(modelBuilder); } } </code></pre> <p><strong>Sample Data :</strong></p> <pre><code>EFRepository&lt;BaseEntity&gt; baseRepository = new EFRepository&lt;BaseEntity&gt;(new MyContext ()); List&lt;BaseEntity&gt; entities = new List&lt;BaseEntity&gt;() { new Entity1() { BaseEntityProp1 = "Entity1Prop1", BaseEntityProp2 = "Entity1Prop2", }, new Entity2() { BaseEntityProp1 = "Entity2Prop1", BaseEntityProp2 = "Entity2Prop2", } }; foreach (var entity in entities) { baseRepository.Insert(entity); baseRepository.Commit(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload