Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

ID-properties are of type int - not always good for existing DB #37

Closed
dkurok opened this issue Aug 15, 2017 · 2 comments
Closed

ID-properties are of type int - not always good for existing DB #37

dkurok opened this issue Aug 15, 2017 · 2 comments

Comments

@dkurok
Copy link

dkurok commented Aug 15, 2017

In ApplicationCore/Entities/BaseEntity.cs you define that the ID-property of an entity is always of type int and always named "Id". I've got a lot of older apps where the ID-property (PK-column in DB) is of different type like string or GUID and has a different name (often Entity+"_ID", but also some complete other naming schemes).
You use the same assumption about the type of ID-property onwards in the interfaces IRepository and IImageService (you assume that the ID of an image is of type int).
Infrastructure/Data/EfRepository then implements this interface and so also here int is backed in.
Is it possible for you to make BaseEntity having a "generic" type for the ID-property and also build in a way of having a different name for the ID-property on a per-entity-base?
Regards
Dietmar

@wyshmily
Copy link

I think this repo is just for demostration, you can write your generic version of EntityBase.
About the name of key field, it can be mapped using ColumnAttribute, so that's not an issue.

Mine looks like this:

    public class BaseEntity<T>
    {
        [Column("id")]
        [Key]
        public T Id { get; set; }

        [Column("ctime")]
        public DateTime CTime { get; set; } = DateTime.Now;

        [Column("utime")]
        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
        public DateTime UTime { get; set; }

        [Column("del")]
        public DeleteState Del { get; set; } = DeleteState.NotDeleted;
    }

@ardalis
Copy link
Collaborator

ardalis commented Oct 24, 2017

Correct (wyshmily). I added a note to the BaseEntity class stating that one could easily use a BaseEntity if desired.

@ardalis ardalis closed this as completed Oct 24, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants