我的数据库有3个表涉及此问题:Employee,Reports,EmployeeReports。由于EmployeeReports仅作为其他2个表的外键,因此EF将实体转换为多对多关系,如下所示:
public Employee()
{
this.Reports = new HashSet<Report>();
}
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
public int EmployerIdFk { get; set; }
public virtual Employer Employer { get; set; }
public virtual Passport Passport { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage "Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Report> Reports { get; set; }
}
}
public partial class Report
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Report()
{
this.Employees = new HashSet<Employee>();
}
public int ReportID { get; set; }
public string ReportName { get; set; }
public string ReportFilePath { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage ("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Employee> Employees { get; set; }
}
我尝试使用多对多安排将所选员工与选定报告关联起来(有一名员工和一份ID为1的报告)。
int eID, rID = 1;
var employee = db.Employees.Include(e => e.Reports).FirstOrDefault(e => e.EmployeeID == eID);
var report = db.Reports;//.Single(e => e.ReportID == rID);
foreach (var item in report)
{
if (item.ReportID == rID)
{ employee.Reports.Add(item); }
}
db.SaveChanges();
我已尝试过对此代码的许多变体,但它总是出错:
" Unable to update the EntitySet 'EmployeeReports' because it has a DefiningQuery
and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation."
如何更正此错误?感谢
答案 0 :(得分:0)
此解决方案的功劳归功于1月15日发布的Kavitha Reddy Unable to update the EntitySet - because it has a DefiningQuery and no <UpdateFunction> element exist这是帖子:
右键单击edmx文件,选择Open with,XML editor
在edmx:StorageModels元素
完全删除DefiningQuery
重命名商店:架构=&#34; dbo&#34;到Schema =&#34; dbo&#34; (否则,代码将生成一个错误,说明名称无效)
我按照这些步骤操作,我的错误消息消失了。令人非常不安的是,这种情况在4月16日的EF中仍然存在。我正在尝试使用多对多EF功能,以确定它在我的应用程序中是否有用。在经历了这种令人沮丧的经历之后,我怀疑我是否会使用它。但是,非常感谢Kavitha解决这个难题!