Entity Framework:来自 DB 的 EF Designer:部分 Class 未按预期工作

Entity Framework : EF Designer from DB : Partial Class not working as expected

我不明白为什么有时有效有时无效。

我正在尝试配置部分 class 以添加更多字段,但控制器无法检测添加到部分 class 的新字段并且视图抛出以下错误:

The associated metadata type for type 'WebApplication1.Models.Table' contains the following unknown properties or fields: countryList, selectedCountryList. Please make sure that the names of these members match the names of the properties on the main type.

所以问题出在部分 class 上。 EF 无法检测到添加到部分 class.

的新字段

模型 class 由 EF

生成
namespace WebApplication1.Models
{
    using System;
    using System.Collections.Generic;
    
    public partial class Table
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Gender { get; set; }
        public Nullable<bool> Java { get; set; }
        public Nullable<bool> Eng { get; set; }
        public string cityCode { get; set; }
    
        public virtual Country Country { get; set; }
    }
}

namespace WebApplication1.Models
{
    using System;
    using System.Collections.Generic;
    
    public partial class Country
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Country()
        {
            this.Tables = new HashSet<Table>();
        }
    
        public long cityId { get; set; }
        public string cityCode { get; set; }
        public string cityName { get; set; }
    
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Table> Tables { get; set; }
    }
}

部分class

namespace WebApplication1.Models
{
    [MetadataType(typeof(TableMetaData))]
    public partial class Table
    {
    }

    public class TableMetaData
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Gender { get; set; }
        public Nullable<bool> Java { get; set; }
        public Nullable<bool> Eng { get; set; }
        public string cityCode { get; set; }
    
        public virtual Country Country { get; set; }

        public SelectList countryList { get; set; }
        public string[] selectedCountryList { get;set; }
    }
}

这是我的控制器,使用 viewBag.list 的原因是控制器无法检测部分 class 字段(即 countryList)。

所以,我现在使用 ViewBag.list,但我应该使用 item.countryList

public ActionResult Create(int? id)
{
    ACDEntities1 db = new ACDEntities1();
    var item = db.Tables.Find(id);
    var CList = db.Countries.ToList();
    viewBag.countryList = new SelectList(CList, "CityCode", "cityName");
    //should use the following
    //item.countryList = new SelectList(CList, "CityCode", "cityName");

    return View(item);
}

查看,我在这里尝试使用部分 class 的字段 selectedCountryList 但它会引发错误。

@Html.DropDownListFor(model => model.selectedCountryList, (IEnumerable<SelectListItem>)viewBag.countryList, "select city", new { @class = "form-control chosen-select", @multiple = true})

// should use this
// @Html.DropDownListFor(model => model.selectedCountryList, //(IEnumerable<SelectListItem>)Model.countryList, "select city", new { @class = "form-control //chosen-select", @multiple = true})

工作代码

部分class

namespace WebApplication1.Models
{
    [MetadataType(typeof(TableMetaData))]
    public partial class Table
    {
        [NotMapped]
        public SelectList countryList { get; set; }
        [NotMapped]
        public string[] selectedCountryList { get;set; }
    }

    public class TableMetaData
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Gender { get; set; }
        public Nullable<bool> Java { get; set; }
        public Nullable<bool> Eng { get; set; }
        public string cityCode { get; set; }
    
        public virtual Country Country { get; set; }

    }
}

这是因为你的部分 class 位置和名字 创建一个名为 Partial 或 ... 并使用主要 class 的确切名称创建您的部分 class 并将您的自定义属性放在那里

  • partial class name must be the exact name of main class
  • 将部分 class 命名空间更改为 WebApplication1.Models,而不是 WebApplication1.Models。您的文件夹名称

此致

我相信你的部分 class 做错了。所有 partial class 部分来自它们在其中指定的各种物理文件被连接在一起成为一个 class 定义 - 所以你的附加字段必须在 部分 class 本身 - 不在“元数据”中 class.

试试这个:

namespace WebApplication1.Models
{
    public partial class Table
    {
        /* you cannot re-define those in your second "partial class" bit.....
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Gender { get; set; }
        public Nullable<bool> Java { get; set; }
        public Nullable<bool> Eng { get; set; }
        public string cityCode { get; set; }
    
        public virtual Country Country { get; set; }
        */ 

        public SelectList countryList { get; set; }
        public string[] selectedCountryList { get;set; }
    }
}

由于这些部分是连在一起的 - 当然,您不能在您手写的部分中定义已在生成的部分中定义的内容。

[MetadataType(typeof(TableMetaData))] 机制旨在让您有机会向现有的 class(例如,代码生成的元数据)添加额外的元数据,以便添加诸如数据注释之类的内容([Required][StringLength] 等)——但该元数据机制并非旨在扩展部分 class 并向其添加其他属性