Can we use enums as typesafe entity ids?

前端 未结 5 810
离开以前
离开以前 2021-02-05 10:21

We are working with a rather large model in a EF 6.1 code first setup and we are using ints for entity ids.

Unfortunately, this is not as typesafe as we would like, sinc

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-05 10:50

    Not sure that this will work in EF but one thing you could do is to have your entities implement IEquatable:

    For example your Blog class:

    public class Blog : IEquatable
    {
        // other stuff
        public bool Equals(Blog other)
        {
            return this.Id.Equals(other.Id);
        }
    }
    

    Alternatively you could use a more flexible ORM such as NHibernate. If this is of interest, let me know and I'll expand my answer.

提交回复
热议问题