Last active
November 10, 2015 12:44
-
-
Save anton-abyzov/ba517c8951874cf72cbe to your computer and use it in GitHub Desktop.
Beware of EF6 (and below) limitations: materialization into domain entity with missing fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EFMaterializationTests | |
{ | |
/// <summary> | |
/// Domain entity (WcChallengeDomain in our case) should have all properties mentioned on materialization of EF6 and below. In this case optional field Description is missing | |
/// on materialization (Select with new WcChallengeDomain) and EF doesn't support it | |
/// </summary> | |
[Test] | |
public void EF6WillThrowExcOnMaterializationOfDomainEntityWithMissingField() | |
{ | |
//arrange | |
var context = new SepDbContext(); | |
Database.SetInitializer<SepDbContext>(null); | |
var data = context.Challenges.Select(x => new WcChallengeDomain() | |
{ | |
Id = x.ChallengeId, | |
Name = x.Name | |
}); | |
//act | |
Mapper.CreateMap<WcChallengeDomain, WcChallengeDto>(); | |
var mappedToDto = data.Map<WcChallengeDomain, WcChallengeDto>(); | |
//assert | |
Assert.Throws<NotSupportedException>(() => mappedToDto.ToList()); | |
} | |
[Test] | |
public void EF6WillMapDomainEntitySuccessfully() | |
{ | |
//arrange | |
var context = new SepDbContext(); | |
Database.SetInitializer<SepDbContext>(null); | |
var data = context.Challenges.Select(x => new WcChallengeDomain() | |
{ | |
Id = x.ChallengeId, | |
Name = x.Name, | |
Description = null // populating default value | |
}); | |
//act | |
Mapper.CreateMap<WcChallengeDomain, WcChallengeDto>(); | |
var mappedToDto = data.Map<WcChallengeDomain, WcChallengeDto>(); | |
//assert | |
Assert.NotNull(mappedToDto.ToList()); | |
} | |
public class SepDbContext : DbContext | |
{ | |
public SepDbContext() : base("SepDB") { } | |
public DbSet<WcChallengeDalDto> Challenges { get; set; } | |
protected override void OnModelCreating(DbModelBuilder modelBuilder) | |
{ | |
modelBuilder.Configurations.Add(new WcChallengeMap()); | |
} | |
} | |
public class WcChallengeMap : EntityTypeConfiguration<WcChallengeDalDto> | |
{ | |
public WcChallengeMap(string schema = "dbo") | |
{ | |
ToTable("WCChallenges", schema); | |
HasKey(x => x.ChallengeId); | |
Property(x => x.Name).HasColumnName("Name").IsRequired().HasMaxLength(50); | |
Property(x => x.Description).HasColumnName("Description").IsOptional().HasMaxLength(800); | |
} | |
} | |
public class WcChallengeDalDto | |
{ | |
public int ChallengeId { get; set; } | |
public virtual string Description { get; set; } | |
public virtual string Name { get; set; } | |
} | |
public class WcChallengeDomain | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public string Description { get; set; } | |
} | |
public class WcChallengeDto | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public string Description { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment