gpt4 book ai didi

c# - Entity Framework 代码优先和链接的实体

转载 作者:太空宇宙 更新时间:2023-11-03 14:51:16 25 4
gpt4 key购买 nike

我有 AspNetUser 实体:

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

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

接口(interface):

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; }
}

还有 2 个实体,每个实体都作为 FK 链接到 AspNetUser(都实现了 IProfile 和 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
}

现在我需要一个需要从 AspNetUser 获取 FirstName/LastName 的方法,如下所示:

        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();

怎么做?我可以向 AspNetUser 添加 2 个属性,例如:

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

但是怎么说,它应该是一个或零个链接,而不创建 UserId/DriverId

即使有了它,我也必须编写如下代码:

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

而且我不喜欢这段代码,因为当我再添加一个实体时,我必须检查所有代码并再添加一个条件。如何正确地做到这一点?可能是使用接口(interface)/继承?

最佳答案

我以前见过重复的代码,但很少见过重复的数据结构。

根据您当前的代码,如果 User.FirstName="A"& Driver.FirstName="B",则结果仅为“A”。如果您有一个名为“Employee”的新实体,如果您不使用当前样式对其进行硬编码,谁知道哪个名字将是 3 个实体中的真正名字?

您当前的代码可以简化为:

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

但它是您要管理的真实数据吗?

我给你另外两个选择:

  1. 将属性添加到根实体,在您的例子中是 AspNetUser:

    public class AspNetUser
    {
    ....
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Nickname { get; set; }
    ....
    }
  2. 管理 1 个实体中的通用配置文件

    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; }
    }

然后在你的代码中:

   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,
.....
})

如果 Driver/User/Empolyee 想要获取 FirstName,只需使用:

Empolyee.AspNetUser.AspNetUserProfile?.FirstName

关于c# - Entity Framework 代码优先和链接的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51674605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com