2

我有一个 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 类实体转换为视图模型的方法。在设计期间,它允许我将 POHeader.UserProfile.FullName 分配给视图模型属性 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被填充。视图模型中的结果值或 OrderBy 是一个空格,这似乎是返回 FullName 属性,但 FirstName 和 LastName 为空,导致 FullName 只返回应该分隔 FirstName 和 LastName 属性的空格, IE

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

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

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

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

4

1 回答 1

0

Not sure this is your issue, but your partial class extends IEntity public partial class UserProfile : IEntity where the auto-gen class does not. I believe these should match exactly, or does C# allow you to do that?

UPDATE I believe you issue is that you are trying the use the derived/calculated property in your linq query, which when you consider mapping this back to your database using EF would not be something it would be able to handle / translate. This discussion seems relevant: Using a partial class property inside LINQ statement

于 2013-01-16T22:35:00.573 回答