【发布时间】:2017-05-17 09:08:56
【问题描述】:
我有 BOM 和 Project 表,它们是多对多的。
以下是我的 BOM
代码public class BOM
{
public int Id { get; set; }
[Required(ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "ThisFieldIsRequired")]
[StringLength(OwnConstants.StringLengthShort)]
public string BOMRevision { get; set; }
[Required(ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "ThisFieldIsRequired")]
[Index("BOM Code", 1, IsUnique = true)]
[StringLength(OwnConstants.StringLengthShort)]
public string BOMCode { get; set; }
[Required(ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "ThisFieldIsRequired")]
[StringLength(OwnConstants.StringLengthShort)]
public string Title { get; set; }
public virtual ICollection<Project> Projects { get; set; }
public virtual ICollection<BOMDetail> BOMDetails { get; set; }
}
以下是我的 Project
代码public class Project
{
public int Id { get; set; }
[Required(ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "ThisFieldIsRequired")]
[Index("Project Code", 1, IsUnique = true)]
[StringLength(OwnConstants.StringLengthShort)]
public string ProjectCode { get; set; }
[Required(ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "ThisFieldIsRequired")]
[Index("Description", 1, IsUnique = true)]
[StringLength(OwnConstants.StringLengthShort)]
public string Description { get; set; }
[Required(ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "ThisFieldIsRequired")]
public int? CustomerId { get; set; }
public virtual Customer Customer { get; set; }
public virtual ICollection<BOM> BOMs { get; set; }
}
我正在尝试将新条目保存到 BOM。但是,当我尝试添加 Project 时,我得到了 null。下面是我要保存的代码。
public override void Save()
{
using (var db = new ApplicationDbContext())
{
var currentID = (Entity as BOM).Id;
var ExistingBOM = db.BOMs.SingleOrDefault(x => x.BOMCode == BOMCodeTextEdit.Text);
if (ExistingBOM != null)
{
XtraMessageBox.Show("Record Exist", "Error", MessageBoxButtons.OK);
return;
}
if (currentID == 0)
{
BOM boms = new BOM()
{
BOMRevision = BOMRevisionTextEdit.Text,
BOMCode = BOMCodeTextEdit.Text,
Title = TitleTextEdit.Text,
};
Project proj = new Project();
int Project_Id = Convert.ToInt32(ProjectsLookUpEdit.EditValue);
proj = db.Projects.SingleOrDefault(x => x.Id == Project_Id);
db.Projects.Attach(proj);
boms.Projects.Add(proj); // <== getting null here
}
db.SaveChanges();
XtraMessageBox.Show("Save Successfully", "Information", MessageBoxButtons.OK);
}
}
我错过了什么吗?
更新代码
int Project_Id = Convert.ToInt32(ProjectsLookUpEdit.EditValue);
var proj = db.Projects.Where(x => x.Id == Project_Id).ToList();
boms.Projects.Add(proj);
db.BOM.Add(boms);
【问题讨论】:
-
尝试
boms.Projects=proj;而不是boms.Projects.Add(proj); -
@M.Wiśnicki 我试过了。我收到一个错误
Severity Code Description Project File Line Suppression State Error CS0266 Cannot implicitly convert type 'ERP_System.Model.Project' to 'System.Collections.Generic.ICollection<ERP_System.Model.Project>'. An explicit conversion exists (are you missing a cast?) -
如果你删除
db.Projects.Attach(proj);并只使用Add()会怎样 -
@M.Wiśnicki 我得到了同样的空异常错误。
-
好的,您尝试再次添加存在
Project,但您需要添加关系才能添加Bom。因此,使用var projs =db.Projects.Where(x => x.Id == Project_Id).ToList();获取项目并将其设置为Bom:boms.Projects=projs。如果您想再次添加项目并将 i 设置为Bom在使用SingleOrDefault();后更改Id
标签: c# mysql entity-framework ef-code-first devexpress