gpt4 book ai didi

c# - Entity Framework 6 更新表并插入外键相关表

转载 作者:太空狗 更新时间:2023-10-30 01:17:47 25 4
gpt4 key购买 nike

不确定如何为这个标题命名,所以如果它不准确,请随时进行编辑。

举个例子,我想要完成的是更新表 foo 中的记录,然后在以 foo 表 PK 作为外键的后续表中创建新记录,考虑一对多关系。

如何更新具有外键约束的表并在这些后续表中创建新的相关记录?

目前我正在使用 Entity Framework 6 将实体添加和附加到上下文并将它们保存到数据库。

编辑

为了进一步阐明我想要实现的目标,下面的对象是我试图保存到上下文中的一个缩减示例。如果我尝试在“Billy Bob”已经创建后添加 intObj 因为他买了一辆新车、另一项服务或他的轮胎已经更换,它将创建一个新的 Billy Bob 记录(重复)和相应的相关表。

        intObj.FirstName = "Billy";
intObj.Lastname = "Bob";
intObj.Important = 100;
intObj.LastSeen = DateTime.Now.Date;
intObj.Cars = new List<Car>{
new Car{
Model = "Commodore",
Make = "Holden",
YearMade = DateTime.Today.Date,
Odometer = 15000,
EmailWordCount = 500,
TyreStatuss = new List<TyreStatus>{
new TyreStatus{
Tyre1 = "Good",
Tyre2 = "Good",
Tyre3 = "Okay",
Tyre4 = "Okay"
}
},
Services = new List<Service>{
new Service{
Cost = "$500",
Time = "2 Days",
Date = DateTime.Today
}
},
}
};

谢谢

最佳答案

在下面的代码片段中,您有 Employee 类,它引用了另外两个实体:Assignment 的集合和一个 Country。
像 EF 、 NHibernate 等 ORM 有一个称为传递持久性的特性,也就是说,如果一个对象(Assignment 和 Country)被一个持久对象(Employee)引用,那么 Assignments 和 Country 最终也会变得持久化,当,在您的 EF 案例中,SaveChanges 方法在上下文中被调用,而您没有明确保存它们。

    public class Employee
{
public virtual Guid Id { get; protected set; }
public virtual string EmployeeNumber { get; set; }
public virtual Country BirthCountry { get; set; }
private ICollection<Assignment> _assignment = new List<Assignment>();
public virtual ICollection<Assignment> Assignments
{
get
{
return _assignment;
}

set
{
_assignment= value;
}
}
}

public class Assignment
{
public virtual Guid Id { get; protected set; }
public virtual DateTime BeginTime { get; set; }
public virtual DateTime EndTime { get; set; }
public virtual string Description{ get; set; }
}

public class Country
{
public virtual Guid Id { get; protected set; }
public virtual string Name { get; set; }
}

//Somewhere in your program
private void SaveAllChanges()
{
_db = new EFContext();
//Creating a new employee here, but it can be one loaded from db
var emp = new Employee { FirstName = "Emp Name",
LastName = "Emp Last", EmployeeNumber = "XO1500"
};
emp.BirthCountry = new Country { Name = "Country1" };
emp.Assignment.Add(new Assignment{ BeginTime = DateTime.Now,EndTime=DateTime.Now.AddHours(1) });

//Only employee is explicitly added to the context
_db.Employees.Add(emp);
//All the objects in the employee graph will be saved (inserted in this case) in the db.
_db.SaveChanges();
}
}

编辑:

这与我上面的代码非常相似,创建“Billy Bob”后您只需要更新它,这包括他购买的任何新服务;

伪代码:

var bob = _db.Clients.SingleOrDefault(c=> c.Id = "Bob Row Id")
//Bob buy a car:
bob.Cars.Add(new Car()...)
//...and change tire 1 from an old car
var car = bob.Cars.SingleOrDefault(c=> c.Id = "Car Row Id")
car.TireStatus.Tire1 = "New"
....

//Persist all changes
//Existing objects will be updated..., and the new ones created in this process will be inserted
_db.SaveChanges()

如果这能阐明您的想法,请告诉我

关于c# - Entity Framework 6 更新表并插入外键相关表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30420152/

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