使用 C# EF4 将基架添加到下拉列表 - 首先使用代码



我正在使用C#.NET,实体框架4.1和代码优先方法。由此我有一堆entities,一个entity与另一个entity有关.它们具有主键/外键关系。

我还使用 ViewModels 来指定哪些属性可以搭建基架,我希望这两个相关实体能够创建一个下拉列表。但这并没有发生。

这只是我的实体关系的一个例子,以说明我的问题。

用户类型实体:

public class UserType
{
    public int UserTypeID { get; set; }
    public string Type { get; set; }
    public virtual ICollection<User> Users { get; set; }
}

用户实体:

public class User
{
    public int UserID { get; set; }
    public int UserTypeID
    public string Username { get; set; }
    public bool IsAdmin { get; set; }
    public virtual UserType UserType { get; set; }
}

我的视图模型:

[JsonObject(MemberSerialization = MemberSerialization.OptOut, IsReference = false)]
[DataContract]
public class UserViewModel : BaseViewModel
{
    [Key]
    public int UserID { get; set; }
    public int UserTypeID { get; set; }
    [required]
    public string Username { get; set; }
}

所以UserViewModel是脚手架。我希望它为UserType创建一个下拉列表。但现在它只为UserTypeID创造了一个input场。如何让它显示一个值为 UserTypes 的下拉列表?

基架不知道视图模型中的 UserTypeId 与实际的 UserType 类相关,因此无法确定从何处获取用户类型的静态列表。如果查看引用的示例,它还包括外键类的导航属性。

相关内容

最新更新