We are using Entity Framework Code First for this project's database.
Our requirements call for a central 'Resource' table, with a single column of ResourceId (uniqueidentifier NOT NULL DEFAULT (newsequentialid())). Various tables would use this table for their ID.
Profile - ProfileId (uniqueidentifier NOT NULL) Organization - OrganizationId (uniqueidentifier NOT NULL) Document = DocumentId (uniqueidentifier NOT NULL)
So, if I create a new Profile record, I would create a new Resource record, and use that sequentially created Guid as the ID for the new Profile record.
The reason for this is to prevent an Id from Profile ever being present as an Id for Organization. (I know that this is most likely improbable, but not impossible.)
Right now we define this with relationships like this:
public class Resource : BaseEntity
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ResourceId { get; set; }
public virtual Profile Profile_ProfileId { get; set; }
//...
}
public class Profile : BaseEntity, IAuditableEntity
{
[Key]
public Guid ProfileId { get; set; }
public virtual Resource Resource { get; set; }
//...
}
public class ProfileMapping : EntityTypeConfiguration<Profile>
{
public ProfileMapping()
{
//Primary key
HasKey(t => t.ProfileId);
//Constraints
Property(t => t.ProfileId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
//...
ToTable("Profile");
//Create Relation
HasRequired(t => t.Resource).WithOptional(t => t.Profile_ProfileId);
}
}
Then, when we create a new Profile we do this (db being an instance of our DBContext):
var res = new Resource();
db.Resource.Add(res);
var newProfile = new Profile{
ProfileId = res.ResourceId,
IsActive = true
};
db.Profile.Add(newProfile);
However, I am wondering, could we define our classes/models to inherit from Resource and get better results?
Have any of you worked with a database structure like this?