Created
April 14, 2020 08:51
-
-
Save Frazi1/964ad01286f55d7ae47b1a8542297215 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using System.Linq; | |
using Microsoft.EntityFrameworkCore; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace PropertyManagementSync.Tests | |
{ | |
[TestClass] | |
public class OwnedTypeTests | |
{ | |
[Owned] | |
public class EmailObject | |
{ | |
public string Value { get; protected set; } | |
public EmailObject(string value) | |
{ | |
Value = value; | |
} | |
} | |
class Entity | |
{ | |
public int Id { get; set; } | |
public EmailObject Email { get; set; } | |
} | |
class TestContext : DbContext | |
{ | |
public DbSet<Entity> Entities { get; set; } | |
protected override void OnModelCreating(ModelBuilder modelBuilder) | |
{ | |
base.OnModelCreating(modelBuilder); | |
modelBuilder.Entity<Entity>() | |
.OwnsOne(e => e.Email, | |
email => email.Property(m => m.Value).HasColumnName("Email")); | |
} | |
} | |
[TestMethod] | |
public void Test() | |
{ | |
using var context = new TestContext(); | |
context.Database.EnsureDeleted(); | |
context.Database.EnsureCreated(); | |
var e = new Entity | |
{ | |
Email = new EmailObject("[email protected]") | |
}; | |
context.Entities.Add(e); | |
context.SaveChanges(); | |
using var ctx2 = new TestContext(); | |
Entity entity = context.Entities.First(); | |
Assert.AreEqual("[email protected]", entity.Email.Value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment