gpt4 book ai didi

c# - 将 Entity Framework 的多对多关系保存到 MySQL

转载 作者:行者123 更新时间:2023-11-30 22:06:43 29 4
gpt4 key购买 nike

我有BOMProject 表,它们是多对多的。

以下是我的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; }
}

下面是我的项目

代码
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 时,结果为空。下面是我要保存的代码。

    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);

最佳答案

您尝试在需要添加的地方添加单个记录 ICollection<T> ,更改您的查询并获取列表而不是单个项目。

var projs =db.Projects.Where(x => x.Id == Project_Id).ToList();

关于c# - 将 Entity Framework 的多对多关系保存到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41440731/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com