- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有以下实体/表:
Board
: 一 block 板可以有很多pinPin
:一个引脚分配给一个板。该实体是抽象的,并且确实有具有不同实现的子实体。属于具有 InheritanceMapping
的父 pin 实体的所有子实体将被保存到 pin 表中并由 Discriminator 列
区分
TaskPin
:这是 pin 的一个子实现。它可以有很多任务。Task
:一个任务分配给一个TaskPin这里有一些代码可以让我的结构更清晰:
[Table]
public class Board : ModelBase
{
private int _boardId;
[Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity"
,CanBeNull = false, AutoSync = AutoSync.OnInsert)]
public int BoardId
{
get { return _boardId; }
set { SetProperty(ref _boardId, value); }
}
private EntitySet<Pin> _pins;
[Association(Storage = "_pins", OtherKey = "_boardId"
,ThisKey = "BoardId", DeleteRule = "CASCADE")]
public EntitySet<Pin> Pins
{
get { return _pins; }
set { _pins.Assign(value); }
}
public Board()
{
_pins = new EntitySet<Pin>(new Action<Pin>(this.addPin)
,new Action<Pin>(this.removePin));
}
private void addPin(Pin pin)
{
NotifyPropertyChanging("Pin");
pin.Board = this;
}
private void removePin(Pin pin)
{
NotifyPropertyChanging("Pin");
pin.Board = null;
}
}
[Table]
[InheritanceMapping(Code = PinType.TaskPin, Type = typeof(TaskPin)
,IsDefault = true)]
public abstract class Pin : ModelBase
{
private int _pinId;
[Column(IsPrimaryKey = true, IsDbGenerated = true
,DbType = "INT NOT NULL Identity", AutoSync = AutoSync.OnInsert)]
public int PinId
{
get { return _pinId; }
set { SetProperty(ref _pinId, value); }
}
[Column]
internal int _boardId;
private EntityRef<Board> _board;
[Association(Storage = "_board", ThisKey = "_boardId"
,OtherKey = "BoardId", IsForeignKey = true, DeleteOnNull = true)]
public Board Board
{
get { return _board.Entity; }
set
{
if (SetProperty(ref _board, value) != null)
{
_boardId = value.BoardId;
}
}
}
[Column(IsDiscriminator = true)]
public PinType Type { get; set; }
public Pin()
{
}
}
public class TaskPin : Pin
{
private EntitySet<Task> _tasks;
[Association(Storage = "_tasks", OtherKey = "_pinId"
,ThisKey = "PinId", DeleteRule = "CASCADE")]
public EntitySet<Task> Tasks
{
get { return _tasks; }
set { _tasks.Assign(value); }
}
public TaskPin()
{
_tasks = new EntitySet<Task>(new Action<Task>(this.addTask)
,new Action<Task>(this.removeTask));
}
private void addTask(Task task)
{
NotifyPropertyChanging("Task");
task.Pin = this;
}
private void removeTask(Task task)
{
NotifyPropertyChanging("Task");
task.Pin = null;
}
}
[Table]
public class Task : ModelBase
{
private int _taskId;
[Column(IsPrimaryKey = true, IsDbGenerated = true
,DbType = "INT NOT NULL Identity"
,CanBeNull = false, AutoSync = AutoSync.OnInsert)]
public int TaskId
{
get { return _taskId; }
set { SetProperty(ref _taskId, value); }
}
[Column]
internal int _pinId;
private EntityRef<Pin> _pin;
[Association(Storage = "_pin", ThisKey = "_pinId"
,OtherKey = "PinId"
,IsForeignKey = true
,DeleteOnNull=true)]
public Pin Pin
{
get { return _pin.Entity; }
set
{
if (SetProperty(ref _pin, value) != null)
{
_pinId = value.PinId;
}
}
}
public Task()
{
}
}
我创建了一个 TaskPin
并将其分配给一个板。然后我创建两个任务并将它们分配给 TaskPin。这确实很好用。当我尝试从 TaskPin
执行一个或多个 Tasks
时出现问题:
private void OnDeleteTasks(object sender, EventArgs e)
{
TaskPin taskPin = pin as TaskPin;
var completedTasks = taskPin.Tasks
.Where(x => x.IsDone == true)
.ToList();
foreach (var task in completedTasks)
{
taskPin.Tasks.Remove(task);
}
}
如果我在 DataContext
对象上调用 SubmitChanges()
,它将设置 TaskPin(继承自 Pin)的 Board 属性
为 null
。
public void Save(Pin pin)
{
// This is empty so no modified members are identified => Correct
var modifiedMembers = db.Pins.GetModifiedMembers(pin);
// Contains just one entry for the deleted Task entity => Correct
var changeSet = db.GetChangeSet();
// This call will immediately set Board property of Pin to null => Wrong!
db.SubmitChanges();
}
我预计 Task
会被删除,因为 DeleteOnNull
设置为 true 但我不知道为什么 Board property
的 Pin也设置为 null
,这将导致 NullPointerExceptio
或 Pin 也被删除。
我对这个主题进行了谷歌搜索,但没有找到任何可以解决我的问题的东西。一种替代方法是防止 Board 属性归零并手动为任务调用 DeleteOnSubmit()
。
最佳答案
在我看来,这种行为是有意为之的。
如果您查看 Task 类中的 Pin 属性,它被设置为在 Null (DeleteOnNull = true) 时删除,然后Task 中的 Pin 将按照 TaskPin 类中的 removeTask() 删除。然后查看Board 类中的removePin() 方法,它将Pin 的Board 设置为null。然后查看Pin 类中的Board 属性,DeleteOnNull 设置为true 这将从Board 中删除strong>Pin 设置为 null 时的实例。
关于c# - WP8 和 Linq to SQL 具有一对多关系 : SubmitChanges() removes wrong entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26225103/
我有这些实体(这只是我为这篇文章创建的抽象): 语言 区 说明 这些是它们之间的引用: 区 * - 1 语言 说明 * - 1 语言 区 1 - 1 说明 如果我这样取: var myFetch =
经过大量谷歌搜索后,除了降级 hibernate 版本之外,我没有找到问题的答案。但我在 2003 年类似的帖子中遇到了这种情况。 问题是什么: //in the first session I d
我听说过 linq to entities 。 Entity Framework 是利用linq to entities吗? 最佳答案 LINQ to Entities 是 Entity Framew
我是 Entity Framework 和 ASP.Net MVC 的新手,主要从教程中学习,对任何一个都没有深入了解。 (我确实有 .Net 2.0、ADO.Net 和 WebForms 方面的经验
如果我编写 LINQ to Entities 查询,该查询是否会转换为提供程序理解的 native 查询(即 SqlClient)? 或者 它是否会转换为实体 SQL,然后 Entity Framew
这个问题已经有答案了: EF: Include with where clause [duplicate] (5 个回答) 已关闭 2 年前。 看来我无法从数据库中获取父级及其子级的子集。 例如...
我开始在一家新公司工作,我必须在一个旧项目上使用 C++ 工作。所以,我忘记了一些 C++ 本身的代码结构。在一个函数中,我在一个函数中有双冒号::,但我不知道如何理解它。 例如,我知道如果我有 EN
我写了一个方法来允许为 orderby 子句传递一个表达式,但我遇到了这个问题。 Unable to cast the type 'System.DateTime' to type 'System.I
简单的问题:LINQ to Entities 和 Entity Framework 有什么区别?到目前为止,我认为这两个名称是用来描述同一个查询的,但我开始觉得事实并非如此。 最佳答案 Entity
我想使用 Entity Framework 。但是,我还要求允许我的用户在我们的系统中定义自定义字段。我想仍然使用 Entity Framework ,而不是使用具有哈希表属性的分部类。 下面是我想到
我正在阅读这个 E.F. 团队博客的这个系列 http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-featu
我正在使用 EF6 开发插件应用程序,代码优先。 我有一个名为 User 的实体的主要上下文。 : public class MainDataContext : DbContext { pub
当我得到最后的 .edmx 时,我遇到了问题。 我收到一条消息说 错误 11007:未映射实体类型“pl_Micro”。 查看设计器 View ,我确实看到该表确实存在。 我怎样才能克服这个消息? 最
我已阅读与使用 Entity Framework 时在 Linq to Entities (.NET 3.5) 中实现等效的 LEFT OUTER JOIN 相关的所有帖子,但尚未找到解决以下问题的方
使用 WCF RIA 服务和 Entity Framework 4. 我有 3 个 DTO:学校、州、区。 州 DTO 有一个地区属性(property),其构成。学校 DTO 有一个国家属性(pro
我有一个 Employee 实体,它继承自一个继承自 Resource 实体(Employee -> Person -> Resource)的 Person 实体。是否可以通过编程方式获取 Emplo
我有一个使用 JPA 的 java 应用程序。 假设我有一个名为 Product 的实体与 name和 price属性(所有实体都有一个 id 属性)。 自然我可以得到一个List相当容易(来自查询或
我有一个 Entity Framework 类,其中有两个指向另一个对象的引用 public class Review { [Key] public int Id {get;s
我是 Symfony 2 的新手,我想知道一些事情: 假设我的项目中有 2 个 bundle 。我想在两个包中使用从我的数据库生成的实体。 我应该在哪里生成实体? (对我来说,最好的方法是在 bund
我想在具有方法和属性的部分类中扩展 EF 实体。我经常这样做。 但是现在我需要将来自该实体的数据与来自其他实体的数据结合起来。因此,我需要能够访问实体 objectcontext(如果附加)来进行这些
我是一名优秀的程序员,十分优秀!