Skip to content

Instantly share code, notes, and snippets.

@Frazi1
Created April 14, 2020 08:51
Show Gist options
  • Save Frazi1/964ad01286f55d7ae47b1a8542297215 to your computer and use it in GitHub Desktop.
Save Frazi1/964ad01286f55d7ae47b1a8542297215 to your computer and use it in GitHub Desktop.
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