实体框架: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]等(,但该元数据机制的设计并不是为了扩展分部类并为其添加额外的属性

这是因为部分类的位置和名称创建一个名为Partial或…的文件夹。。。并使用主类的确切名称创建您的分部类,并将您的自定义属性放置在中

  • 分部类名必须是主类的确切名称
  • 将分部类命名空间更改为WebApplication1.Models,而不是WebApplication1.Models。您的文件夹名称

向致以最良好的问候

最新更新