简体   繁体   中英

How should I create this relationship in Entity Framework?

In my previous question, I've mentioned that I'm developing a Point of Sale application. The platforms I use are WPF, MVVM and Entity Framework (code-first). Since Entity Framework code-first has its own logic to map entities and relations, I'm confused about a case.

I have an EntityBase class:

public class EntityBase
{
    public DateTime? CreatedAt { get; set; }
    public User CreatedBy { get; set; }
    public DateTime? UpdatedAt { get; set; }
    public User UpdatedBy { get; set; }
}

public class User : EntityBase
{
    public int UserId { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
}

Since User class has EntityBase class members, will it arise problems at database side?

Or should I keep the data like this:

public class EntityBase
{
    public DateTime? CreatedAt { get; set; }
    public int CreatedByUserId { get; set; }
    public DateTime? UpdatedAt { get; set; }
    public int UpdatedByUserId { get; set; }
}

Create your models in this way:

public class EntityBase
    {
        public DateTime? CreatedAt { get; set; }
        public int CreatedById { get; set; }
        [ForeignKey("CreatedById")]
        public User CreatedBy { get; set; }
        public DateTime? UpdatedAt { get; set; }
        public int UpdatedById { get; set; }
        [ForeignKey("UpdatedById")]
        public User UpdatedBy { get; set; }
    }
    public class User
    {
        public int UserId { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM