Reputation: 1157
I've a base class User that's used by EF Core and here's the configuration for that as follows:
public class UserConfiguration: IEntityTypeConfiguration<User>
{
public void Configure(EntityTypeBuilder<User> builder)
{
builder.ToTable("Users");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id).HasColumnName("UserId");
builder.Property(x => x.FirstName).HasColumnName("FirstName");
builder.Ignore(x => x.ClientVersion);
builder.Ignore(x => x.LastUsedByClient);
}
}
Base Class:
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string ClientVersion { get; set; }
public string LastUsedByClient { get; set; }
}
Now I've a requirement to extend the class properties without changing anything in the configuration.
public class UserRequest : User
{
public DateTime UpdateDate { get; set; }
public bool Status { get; set; }
}
How can I ignore the properties that are in the new class inherited from the base class User? My plan is to use the new class as follows:
Context.Users.Add(UserRequest); //To add data in database with the user context
Upvotes: 0
Views: 102
Reputation: 344
First, I would recommend you to read more about Inheritance Mapping Strategies in .net. You can find on this here
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.ToTable("Users");
modelBuilder.Entity<UserRequest>()
.ToTable("UserRequests")
.HasBaseType<User>();
base.OnModelCreating(modelBuilder);
}
This will create table per type we have two different tables in the database don't forget to run migration and update the database. Then you can create the same fluent validation on the UserRequest Type.
I don't know if you are really need to create inheritance relationship. In my opinion it's enough to add the both properties inside the User Class.
Upvotes: 0
Reputation: 112672
I think that your model is wrong. A UserRequest
should not inherit from User
but have a relationship with User
. I.e., a User
should have a 1-to-n relationship to UserRequest
.
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string ClientVersion { get; set; }
public string LastUsedByClient { get; set; }
// Navigation property to children
public List<UserRequest> UserRequests { get; set; }
}
public class UserRequest
{
public int Id { get; set; } // Add this PK to UserRequest
public DateTime UpdateDate { get; set; }
public bool Status { get; set; }
// Navigation properties to parent
public int UserId { get; set; }
public User User { get; set; }
}
Generally we can say that an inheriting class is a of the type of its parent class as well. Example:
public class User { ... }
public class SuperUser : User
{
public void DeleteUser(User user)
{
...
}
}
Here SuperUser
inherits from User
. The SuperUser
is a User
, but with additional privileges.
var super = new SuperUser();
if (super is User) {
// Always true, if-statement is obsolete.
}
User selectedUser = ...;
User user = ...;
if (user is SuperUser superUser) {
// Executes only if user is a SuperUser
superUser.DeleteUser(selectedUser);
}
But a UserRequest
is not a User
!
See also:
Upvotes: 4