c# - 实体框架:来自 DB 的 EF 设计器:部分类未按预期工作
问题描述
我不明白为什么这有时有效,有时无效。
我正在尝试配置部分类以添加更多字段,但控制器无法检测到添加到部分类的新字段,并且视图会引发以下错误:
类型“WebApplication1.Models.Table”的关联元数据类型包含以下未知属性或字段:countryList、selectedCountryList。请确保这些成员的名称与主类型上的属性名称匹配。
所以问题出在部分类上。EF 无法检测到添加到部分类的新字段。
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; }
}
}
部分类
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 的原因是控制器无法检测到部分类字段(即 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);
}
查看,这里我试图使用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})
工作代码
部分类
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; }
}
}
解决方案
我相信你的部分课程做错了。来自它们指定的各种物理文件的所有部分类部分都连接到一个类定义中 - 因此您的附加字段必须在部分类本身中 - 而不是在“元数据”类中。
试试这个:
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))]
机制旨在让您有机会向现有类(例如,代码生成的类)添加额外的元数据,以便添加数据注释(等)之类的东西[Required]
-[StringLength]
但元数据机制并非旨在扩展部分类并向其添加其他属性
推荐阅读
- python - 从 TkInter Canvas 中删除所有内容并在 mainloop 中放置新项目
- ruby - 为什么我的代码输出枚举值而不是字符串?
- python - 当 as_index=False 时 groupby.first、groupby.nth、groupby.head 有什么不同
- c# - 如何修改模型并使用 ASP.net Core/Razor 中的计算值重新验证它?
- java - 如何修复获取空的 .txt 文件
- javascript - JQuery-steps 动态地将步骤添加到表单并将所有对象传递给控制器
- python - 日期时间将语言从英语更改为乌克兰语 python?
- parsing - 如何使用纯函数式编程在 ocaml 中制作非上下文解析器?
- javascript - 未捕获的 DOMException:无法构造“Worker”:papaparse.mi.js 处的脚本
- docker - 无法在 docker 容器中运行 pytests