I'm facing a problem using EF. I have the following situation:
Table User: Username, Password, RoleId, IsActive, CreatedDate, ActivedDate
Table Admin: Username, Name
Table Staff: Username, Name, Position, Phone
From this database schema i'd like to generate the following entity by merge tables data:
public class User
{
[Key]
public string Username { get; set; }
public string Password { get; set; }
public int RoleId { get; set; }
public bool IsActive { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime ActivedDate { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public string Position { get; set; }
[ForeignKey("RoleId")]
public Role Role { get; set; }
}
configuration class:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.Map(map =>
{
map.Properties(p => new
{
p.Username,
p.Password,
p.RoleId,
p.IsActive,
p.CreatedDate,
p.ActivedDate
});
map.ToTable("User");
})
.Map(map =>
{
map.Properties(p => new
{
p.Username,
p.Name
});
map.ToTable("Admin");
}).Map(map =>
{
map.Properties(p => new
{
p.Username,
p.Name,
p.Phone,
p.Position
});
map.ToTable("Staff");
});
base.OnModelCreating(modelBuilder);
}
I've tested it but it doesn't work as expected. I always get this message:
Properties for type 'User' can only be mapped once. The non-key property 'Name' is mapped more than once. Ensure the Properties method specifies each non-key property only once.
Am I missing something?