简体   繁体   中英

Entity Framework Code First and linked entities

I have AspNetUser entity :

public partial class AspNetUser
{
    public string Id { get; set; }

    [Required]
    [StringLength(256)]
    [Index(IsClustered = false, IsUnique = true)]
    public string UserName { get; set; }
    ....

Interfaces:

public interface IProfile
{
    string FirstName { get; set; }
    string LastName { get; set; }
    string Nickname { get; set; }
}

public interface IMembershipEntity
{
    string AspNetUserId { get; set; }
    AspNetUser AspNetUser { get; set; }
}

And 2 entities, each of them has link to AspNetUser as FK (both implement IProfile and IMembershipEntity):

public partial class Driver : IMembershipEntity, IProfile
{

    #region IProfile
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Nickname { get; set; }
    #endregion

    #region IMembershipEntity
    public string AspNetUserId { get; set; }
    [ForeignKey("AspNetUserId")]
    public virtual AspNetUser AspNetUser { get; set; }
    #endregion

   ... driver fields


public class User : BaseEntity, IMembershipEntity, IProfile
{
    #region IMembershipEntity
    public string AspNetUserId { get; set; }
    public virtual AspNetUser AspNetUser { get; set; }
    #endregion

    #region IProfile
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Nickname { get; set; }
    #endregion

    .... user fields
}

right now I need a method which need get FirstName/LastName from AspNetUser, like this:

        var element = (from i in db.AspNetUsers
                    select new UserElementDomain()
                    {
                        UserId = i.Id,
                        CompanyName = i.Company.Name,
                        FirstName = (get FirstName here),
                        LastName = (get LastName here),
                        RoleName = i.AspNetRoles.FirstOrDefault().Name,
                        SMSnumber = i.SMSnumber,
                        UserName = i.UserName,
                        .....
                    }).FirstOrDefault();

How to do it? I can add 2 properties to AspNetUser, like:

    public virtual User User { get; set; }
    public virtual Driver Driver { get; set; }

but how to say, that it should be one or zero link, without creating UserId/DriverId

And even with it, then I have to write code like:

FirstName = (i.User!=null)? i.User.FirstName : ((i.Driver!=null)?i.Driver.FirstName : String.Empty)

and I don't like this code, because when I add one more Entity I have to review all code and add one more condition. How to do it correctly? Probably, using interfaces/inheritance?

I saw duplicated code before, but rarely saw duplicated data structure.

by your current code, if User.FirstName="A" & Driver.FirstName="B", then result is "A" only. if you have a new entity called "Employee", who knows which name will be the real firstname in 3 of entities, if you don't hard code it with your current style?

your current code could simplify to :

i.User?.FirstName ?? i.Driver?.FirstName ?? i.Whatever?.FirstName ?? String.Empty

but is it the real data you want to manage?

I give you 2 other options:

  1. add properties to root entity, it's AspNetUser in your case:

     public class AspNetUser { .... public string FirstName { get; set; } public string LastName { get; set; } public string Nickname { get; set; } .... } 
  2. Manage common profiles in 1 entity

     public class AspNetUser { .... public virtual AspNetUserProfile AspNetUserProfile { get; set; } } public class AspNetUserProfile { [Key] public string Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Nickname { get; set; } .... [ForeignKey("Id")] public virtual AspNetUser AspNetUser { get; set; } } 

then in your code:

   db.AspNetUsers.Select(i => new UserElementDomain()
                {
                    UserId = i.Id,
                    CompanyName = i.Company.Name,
                    FirstName = i.AspNetUserProfile?.FirstName,
                    LastName = i.AspNetUserProfile?.LastName,
                    RoleName = i.AspNetRoles.FirstOrDefault().Name,
                    SMSnumber = i.SMSnumber,
                    UserName = i.UserName,
                    .....
                })

if Driver/User/Empolyee want to get FirstName, just use:

Empolyee.AspNetUser.AspNetUserProfile?.FirstName

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