I have two "owned" types referenced from ApplicationUser
(which inherits from IdentityUser
):
using System;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
namespace OwnedEntityTest.Data
{
public class ApplicationUser : IdentityUser
{
public PersonalName Name { get; set; }
public ValidationToken ValidationToken { get; set; }
}
[Owned]
public class PersonalName
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
[Owned]
public class ValidationToken
{
public int ValidationCode { get; set; }
public DateTime ExperiationDateUTC { get; set; }
}
}
When I add a new migration, the scaffolder complains:
Cannot use table 'IdentityUser' for entity type 'ValidationToken' since it is being used for entity type 'PersonalName' and there is no relationship between their primary keys.
Is this just a bug, or am I doing something subtly wrong (or not understanding something about owned types)?
Yes, you can recreate this problem:
ASP.NET Core Web Application
project templateWeb Application
Change Authentication
and select Individual user accounts
update-database
to update the model snapshot.ApplicationDbContext
, add the reference to ApplicationUsers:public DbSet<ApplicationUser> ApplicationUsers { get; set; }
add-migration m_001
EDIT:
Microsoft has confirmed that this is a bug.
In the mean time, you can use the fluent method to configure this.
[Owned]
attributes from the data modelConfigure the relationship by overriding the OnModelCreating
method on your ApplicationDbContext
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>(b =>
{
b.OwnsOne(e => e.Name);
b.OwnsOne(e => e.ValidationToken);
});
}