尝试在 LINQ to SQL 中插入根时插入的现有引用实体
我在 LINQ to SQL 中有两个模型。第一个是员工,第二个是职位。 我已经在位置表中插入了位置数据。该数据将由 Employee 模型引用。
Position {
int id, //auto increament
string name,
string description,
}
Employee {
int id,
string name,
Position position,
}
我要创建员工时的逻辑是:
- 我创建 EmployeCreate 并分配员工姓名和职位 ID
- 使用职位存储库按 Id 查找职位。
- 创建员工模型分配第二步中创建的职位模型
- 使用员工存储库插入员工并提交更改
我认为我的步骤是正确的,但是创建的职位再次插入到数据库中,通常不会插入,因为数据已经存在于数据库中数据库。
我的代码片段服务如下:
EmployeService {
//property and constructor
AddEmployee(EmployeeCreate command) {
var employee = EmployeeFactory.Create(command);
var possition = _positionRepository.GetById(command.possitionId);
using (var tx = new TransactionScope())
{
employee.Position = possition;
_employeeRepository.InsertOnSubmit(employee);
_employeeRepository.SubmitChanges();
tx.Complete();
}
}
}
编辑 我创建通用 Linq 存储库。员工和职位是通用 Linq 存储库的扩展。
public class LinqRepository<T> : IRepository<T>, IRepository where T : class
{
protected readonly DataContext _dataContext;
public LinqRepository(IDataContextProvider dataContextProvider)
{
_dataContext = dataContextProvider.DataContext;
}
}
I have two models in LINQ to SQL. First is Employee and the second is Position.
There is Position data that I already inserted in Position table. This data will be referenced by Employee model.
Position {
int id, //auto increament
string name,
string description,
}
Employee {
int id,
string name,
Position position,
}
The logic when I want to create employee is:
- I Crate EmployeCreate and Assign employee name, and position ID
- Find the position by Id using position repository.
- Create Employee model assign the founded position model from step two
- Insert the employe using employee repository and submit changes
I think my steps is correct, But The founded positions is insert in to database again, normally It is not inserted because the data already exists in the database.
My snippet service as follows:
EmployeService {
//property and constructor
AddEmployee(EmployeeCreate command) {
var employee = EmployeeFactory.Create(command);
var possition = _positionRepository.GetById(command.possitionId);
using (var tx = new TransactionScope())
{
employee.Position = possition;
_employeeRepository.InsertOnSubmit(employee);
_employeeRepository.SubmitChanges();
tx.Complete();
}
}
}
EDIT
I create generic Linq repository. Employee and Position is extends the generic Linq repository.
public class LinqRepository<T> : IRepository<T>, IRepository where T : class
{
protected readonly DataContext _dataContext;
public LinqRepository(IDataContextProvider dataContextProvider)
{
_dataContext = dataContextProvider.DataContext;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非您尝试跨多个上下文提交更改,否则您不需要此处的 TransactionScope。 SubmitChanges 在内部为请求创建一个事务,以涵盖作为提交更改的一部分请求的多个插入/更新/删除操作。您需要显式管理上下文的唯一时间是当您有调用跨越的多个上下文/连接时。
您看到的行为表明,该职位可能是通过上下文获取的,而不是您将员工插入 _employeeRepostiory 时使用的上下文。
You shouldn't need the TransactionScope here unless you are trying to submit changes across multiple contexts. SubmitChanges internally creates a transaction for the request to cover the multiple insert/update/delete operations requested as part of the submitchanges. The only time you need to explicitly manage the context is when you have multiple context/connections that are spanned by the call.
The behavior you are seeing indicates that the Position may be fetched by a context other than the one that you are using when inserting the employee into the _employeeRepostiory.