c# - EF 5 派生类无法访问部分类的属性

标签 c# asp.net-mvc entity-framework

我有一个 EF 5/MVC 4,我正在处理的数据库优先项目,并为我的应用程序的某些逻辑功能区域(UserManagement、CompanyManagement、InventoryManagement、OrderManagement)创建了单独的 edmx 文件。 UserProfile 表位于所有 edmx 文件中,因为它用于连接 CreatedById 和 ModifiedById 表,因此您可以获得实际的用户名。但我不想在每个版本的 UserProfile 表中创建额外的属性和数据注释,所以我继承了 UserProfile 的(我称之为)“原始版本”,它位于我添加额外属性的 UserManagement 区域中并添加我的数据注释。好的,还和我在一起吗?

好的,这是 EF 5 创建的原始 UserProfile 类(在 UserManagement 区域/文件夹中):

namespace OTIS.domain.UserMgmt
{
    using System;
    using System.Collections.Generic;

    public partial class UserProfile
    {
        public UserProfile()
        {
            this.webpages_Roles = new HashSet<webpages_Roles>();
        }

        public int UserId { get; set; }
        public Nullable<int> AccountId { get; set; }
        public string UserName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }

        public virtual webpages_Membership webpages_Membership { get; set; }
        public virtual ICollection<webpages_Roles> webpages_Roles { get; set; }
    }
}

这是我用来添加其他属性和数据注释的部分类:

namespace OTIS.domain.UserMgmt
{
    [MetadataType(typeof(UserProfileMD))]
    public partial class UserProfile : IEntity
    {
        public int Id
        {
            get
            {
                return this.UserId;
            }
        }

        public string FullName
        {
            get
            {
                return this.FirstName + " " + this.LastName;
            }
        }

        public string EntityDescription
        {
            get
            {
                return this.FullName;
            }
        }

        public class UserProfileMD
        {
            public int UserId { get; set; }

            [Required]
            [Display(Name = "User Name")]
            public string UserName { get; set; }

            [Required]
            [Display(Name = "First Name")]
            public string FirstName { get; set; }

            [Required]
            [Display(Name = "Last Name")]
            public string LastName { get; set; }

            [Display(Name = "Name")]
            public string FullName { get; set; }

            [Display(Name = "Email")]
            public string Email { get; set; }

        }
    }
}

现在我正在使用 InventoryMgmt edmx 中的 UserProfile 版本,因此我创建了一个同名的部分类并从上面继承:

namespace OTIS.domain.InventoryMgmt
{
    [MetadataType(typeof(OTIS.domain.UserMgmt.UserProfile.UserProfileMD))]
    public partial class UserProfileInvnMgmt : UserProfile
    {

    }
}

现在 InventoryMgmt edmx 中还有 PO header 。这是 EF 生成的类(注意它引用了 UserProfileInvnMgmt UserProfile,它是 CreatedById 的外键表):

namespace OTIS.domain.InventoryMgmt
{
    using System;
    using System.Collections.Generic;

    public partial class POHeader
    {
        public POHeader()
        {
            this.PODetails = new HashSet<PODetail>();
        }

        public int Id { get; set; }
        public int FacilityId { get; set; }
        public int VendorId { get; set; }
        public int StatusId { get; set; }
        public string Notes { get; set; }
        public int CreatedById { get; set; }
        public System.DateTime CreatedOn { get; set; }
        public int ModifiedById { get; set; }
        public System.DateTime ModifiedOn { get; set; }

        public virtual ICollection<PODetail> PODetails { get; set; }
        public virtual Vendor Vendor { get; set; }
        public virtual POHeaderStatus POHeaderStatus { get; set; }
        public virtual UserProfileInvnMgmt UserProfile { get; set; }
        public virtual UserProfileInvnMgmt UserProfile1 { get; set; }
    }
}

现在我有一个方法可以将 POHeader 类实体转换为 View 模型。在设计时,它允许我将 POHeader.UserProfile.FullName 分配给 View 模型属性 OrderedBy。

public IEnumerable<POHeadersListViewModel> ConvertClassToViewModel(IEnumerable<POHeader> purchaseOrders)
    {
        IEnumerable<POHeadersListViewModel> poGrid =
            from l in purchaseOrders.ToList()
            select new POHeadersListViewModel()
            {
                Id = l.Id,
                VendorName = l.Vendor.VendorName,
                OrderedBy = l.UserProfile.FullName,
                OrderDate = l.CreatedOn,
                Status = l.POHeaderStatus.DisplayName
                //OwnerName = l.Customer == null ? "" : l.Customer.CustomerName
            };

        return poGrid;
    }

但是,当您运行此代码时,不会为 OrderedBy 分配任何值,并且如果在调试时展开 IEnumerable purchaseOrders 变量,您会看到 UserProfile,但如果展开它,则看不到 FullName 属性,但是FirstName 和 LastName 已填充。 View 模型中的结果值或 OrderBy 是单个空格,这似乎正在返回 FullName 属性,但 FirstName 和 LastName 为空,导致 FullName 仅返回应该分隔 FirstName 和 LastName 属性的空格,即

public string FullName
    {
        get
        {
            return this.FirstName + " " + this.LastName;
        }
    }

但请注意,如果我这样做:

OrderedBy = l.UserProfile.FirstName + " " + l.UserProfile.LastName,

有效,因此 FirstName 和 LastName 不为空。关于为什么没有正确返回 FullName 的任何想法?一般来说,处理多个版本的 UserProfile 的最佳方式是什么?

最佳答案

不确定这是您的问题,但您的分部类扩展了 IEntity public partial class UserProfile : IEntity 而 auto-gen 类没有。我相信这些应该完全匹配,或者 C# 允许您这样做吗?

更新 我相信你的问题是你正在尝试在你的 linq 查询中使用派生/计算的属性,当你考虑使用 EF 将其映射回你的数据库时,它不会是什么能够处理/翻译。此讨论似乎相关:Using a partial class property inside LINQ statement

关于c# - EF 5 派生类无法访问部分类的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14368490/

相关文章:

c# - 从 Lua 5.1 移植到 5.2

asp.net - Razor 应用程序中出现错误 "Index was out of range"

c# - 将 Jasig 的中央身份验证服务 (CAS) 与 ASP.NET MVC 一起使用 4/5

c# - MVC 访问 Controller 中的应用程序变量

asp.net-mvc - ASP.net MVC 4,连接字符串

c# - OWIN/武士刀 & BasicAuthentication

c# 将二进制数据读取到字符串中

c# - LoginStatus 控件无法使用数据库进行基本验证的日志记录

c# - 为什么我在 Fluent API 中找不到 IsIndependent()?

c# - 在 quartz EF6 中使用 DBContext