I have a problem with mapping properties on value objects/complex types to entities. I have a user entity which has a complex type property called Credential, which in turn has properties: Email, UserName, Password and SecurityStamp. I am trying to map properties from complex type to entities, and the source code is shown below:
public class User
{
public string Id { get; protected set; }
public Credential Credential { get; protected set; }
// unrelated properties and methods omitted for simplicity
}
public sealed class Credential: ValueObject<Credential>
{
public string Email { get; private set; }
public string UserName { get; private set; }
public string Password { get; private set; }
public string SecurityStamp { get; private set; }
public Credential() { }
public Credential(string email, string userName, string password, string securityStamp)
{
Email = email;
UserName = userName;
Password = password;
SecurityStamp = securityStamp;
}
public class CoreContext: DbContext
{
public IDbSet<User> Users { get; set; }
public CoreContext(string connectionString) : base(connectionString) {}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<CoreContext>(null);
modelBuilder.ComplexType<Credential>().Property(ucr => ucr.Email).HasColumnName("Email");
modelBuilder.ComplexType<Credential>().Property(ucr => ucr.UserName).HasColumnName("UserName");
modelBuilder.ComplexType<Credential>().Property(ucr => ucr.Password).HasColumnName("Password");
modelBuilder.ComplexType<Credential>().Property(ucr => ucr.SecurityStamp).HasColumnName("SecurityStamp");
modelBuilder.Entity<User>().Property(u => u.Credential.Email).HasColumnName("Email");
modelBuilder.Entity<User>().Property(u => u.Credential.UserName).HasColumnName("UserName");
modelBuilder.Entity<User>().Property(u => u.Credential.Password).HasColumnName("Password");
modelBuilder.Entity<User>().Property(u => u.Credential.SecurityStamp).HasColumnName("SecurityStamp");
}
}
As you see, I was trying to map complex type properties to the entity. I started by configuring Credential as a complex type, and then configure the properties on entity User. However, I am receiving the following error:
The 'Credential.Email' property does not exist or is not mapped for the type 'User'.
I dont understand why this happens. I've done everything I can to configure complex type mapping, why it wont work? Anyone have ideas on what I may have done wrong?
Not been able to reproduce this:
I've got this after add-migration with the same setup you have:
public override void Up()
{
CreateTable(
"dbo.Users",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Email = c.String(),
UserName = c.String(),
Password = c.String(),
SecurityStamp = c.String()
})
.PrimaryKey(t => t.Id);
}
The only thing I've removed is your base class on Credential: ValueObject because I don't have it. Everything looks good so maybe there's something wrong with that class?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.