Skip to main content
1 of 2
Esteban
  • 111
  • 4

This seems about right.

There is only one thing I'd like to point out.

I'd change this:

public interface IEntity {
    int Id { get; }
}

public class Entity : IEntity {
    public virtual int Id { get; set; }
}

All your entities are assumed to have only one Id, ruling out every composite key.

Let's think about this common scenario:

User.cs

public class User {
    public int Id { get; set; }
    public string Username { get; set; }
}

Email.cs

public class Email {
    public int Id { get; set; }
    public string Address { get; set; }
}

UserEmail.cs

public class UserEmail {
    public int UserId { get; set; }
    public int EmailId { get; set; }
    public bool IsPrimary { get; set; } //Adding this prop requires to map this entity.
}

Given your base entity and entity interface you won't be able to handle composite keys. Also, keep in mind that Entity Framework will handle by itself the many to many relationships as long as the table contains no more than the Id's, when you add any other property you will have to add that entity to your project.

My advice is to move the Id to every entity, if you need/want to keep your base entity interface and class as a constraint, you could, but you dont really need the interface and the class.

Like:

public interface IEntity {
    int Id { get; }
}

public class Entity : IEntity {
    public virtual int Id { get; set; }
}

public class User : Entity, IUser {
    public int Id { get; set; }
    // ...
}
Esteban
  • 111
  • 4