- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是我不相信的代码。请检查我如何将实体集合作为参数传递。
public ExamProduced GetExamProduced(XElement xml)
{
var examProduced = new ExamProduced
{
ExamProducedID = (int)xml.Attribute("ExamID"),
Date = (DateTime)xml.Attribute("Date"),
Seed = (int)xml.Attribute("Seed"),
//Exercises = GetExercises(xml)
};
GetExercises(xml, examProduced.Exercises);
return examProduced;
}
public void GetExercises(XElement xml, EntityCollection<Exercise> entityCollection)
{
var objs =
from objective in xml.Descendants("Objective")
where (bool)objective.Attribute("Produced")
let id = (int)objective.Attribute("ID")
let id2 = (Objective)entityService.Objectives.Where(o => o.ObjectiveID == id).FirstOrDefault()
select new Exercise
{
Objective = id2,
MakeUp = ...
Quantify = ...
Score = ...
};
foreach (var exercise in objs)
{
entityCollection.Add(exercise);
}
}
否则,我会收到错误消息。像这样使用这段代码。
public ExamProduced GetExamProduced(XElement xml)
{
var examProduced = new ExamProduced
{
ExamProducedID = (int)xml.Attribute("ExamID"),
Date = (DateTime)xml.Attribute("Date"),
Seed = (int)xml.Attribute("Seed"),
Exercises = GetExercises(xml)
};
return examProduced;
}
public EntityCollection<Exercise> GetExercises(XElement xml)
{
var objs =
from objective in xml.Descendants("Objective")
where (bool)objective.Attribute("Produced")
let id = (int)objective.Attribute("ID")
select new Exercise
{
ExerciseID = id,
MakeUp = (bool)objective.Attribute("MakeUp"),
Quantify = (byte)(int)objective.Attribute("Quantify"),
Score = (float)objective.Elements().Last().Attribute("Result")
};
var entityCollection = new EntityCollection<Exercise>();
foreach (var exercise in objs)
entityCollection.Add(exercise);
return entityCollection;
}
我得到的错误如下:
InvalidOperationException was unhandled.
The object could not be added to the EntityCollection or EntityReference. An object that is attached to an ObjectContext cannot be added to an EntityCollection or EntityReference that is not associated with a source object.
最佳答案
我希望从评论中我能正确理解你......如果没有,我会更新这个答案。
首先,您的 EntityCollection<Exercise> GetExercises(XElement xml)
是行不通的。正如错误消息所说,您不能构造随机 EntityCollection
像那样:EntityCollection
需要将对象附加到上下文,因为它会自动将其列表与上下文同步。而且由于您没有说要将它附加到任何地方,所以它不会起作用。让它工作的唯一方法是避免创建 EntityCollection
首先。
你的 void GetExercises(XElement xml, EntityCollection<Exercise> entityCollection)
程序可以工作。但是,您需要确保实际拥有 EntityCollection<Exercise>
能够调用它的实例。您创建 ExamProduced
的方式对象,当您从 GetExamProduced
返回时,它还没有附加到上下文中, 所以不可能有 EntityCollection<Exercise>
那时它的属性(property)。
也许让事情正常进行的最简单方法是将您的上下文传递给 GetExamProduced
方法,并让它们自动附加到上下文。我假设这是一个常见的 ObjectContext
, 但您可以根据需要更新它:
public ExamProduced GetExamProduced(XElement xml, YourContext context)
{
var examProduced = new ExamProduced()
{
ExamProducedID = (int)xml.Attribute("ExamID"),
Date = (DateTime)xml.Attribute("Date"),
Seed = (int)xml.Attribute("Seed")
};
context.ExamsProduced.Attach(examProduced);
LoadExercises(xml, context, examProduced);
// examProduced.Exercises should be available at this point
return examProduced;
}
public void LoadExercises(XElement xml, YourContext context, ExamProduced examProduced)
{
foreach (var exercise in
from objective in xml.Descendants("Objective")
where (bool)objective.Attribute("Produced")
let id = (int)objective.Attribute("ID")
let id2 = (Objective)entityService.Objectives.Where(o => o.ObjectiveID == id).FirstOrDefault()
select new Exercise
{
ExamProduced = examProduced,
Objective = id2,
MakeUp = ...
Quantify = ...
Score = ...
}))
{
context.Exercises.Attach(exercise);
}
}
我不知道这些是应该添加到数据库中的新对象,还是这些对象应该已经存在于数据库中。我假设是后者。如果是前者,.Attach
应更新为 .AddObject
在两个地方。
这就是你要找的,还是我理解错了?
关于c# - 如何改进有关 EntityCollections<TEntity> 的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8541539/
这应该很明显,但由于某种原因我无法弄清楚。所以在我的通用存储库类中,我有一个更新方法来更新实体: public void Update(TEntity entity) where TEntity :
我正在围绕 EF Core 编写一个小型包装器方法 DbSet .我有以下方法: public Task> GetAsync(Func, IQueryable> getFunction) {
我正在寻找一种将 Entity 和 TEntity 作为参数传递的方法,以便我可以将此方法用作通用方法。类似的东西: private void AttachSingleEntity(Entit
我想创建 Func, IOrderedQueryable>来自 SortingItems 的列表动态地。我的意思是我想创建以下表达式: entity => entity.OrderBy(c => c.
我正在使用这里提到的模式 http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-rep
我偶然发现了下一个问题......我有数据库上下文: // For support unit testing... public interface IDbContext : IDisposable
我有一个静态类 DataSource从文件中提取请求的数据并将其作为 List. 返回在 TestRepository (下),我正在使用通用 TEntity , 确定其类类型并从 DataSourc
我正在尝试将 DbLinq 与 SQLite 数据库一起使用,但在尝试转换 ITable 时遇到了问题作为Queryable . 在 DbLinq ( Issue 211 ) 中有一个已知的错误,这可
在 ms 文档中 Razor pages tutorial DbContext 有一个 DbSet Students 集 public class SchoolContext : DbContext
概述/描述 简单:从TEntity 中派生 的运行时类型对象的多态删除添加到 ObjectSet不会提高 IBindingList.ListChanged IBindingList 上的事件Objec
尝试将 Linq to SQL 用于我在家进行的一个小项目。我使用 dbmetal.exe(来自 DBLinq 项目)针对本地 MySQL 数据库生成了上下文代码和我的所有实体类。 一切都运行良好,但
似乎最简单的事情有时很难弄清楚...... 许多代码引用都具有 TEntity 类型,用于对实体数据模型中的实体进行一般处理。我试图在我的代码中使用它并得到:“未知类型'TEntity'”是什么给出的
更新: 我准备了一个通用函数来捕获数据库行以生成列表。我的方法就是这样的。 public class GenericDataAccess : IGenericRepository where
我想写一个通用的方法来返回任何模型,如产品、销售等。像这样的东西(.net 3.5;我不使用 Entity Framework ) public class ProductRepository : I
我想对存储库中的检索方法进行单元测试,该存储库具有模拟的 DbContext,但我无法将模拟的 DbSet 值设置到存储库。 存储库如下所示: public class ChangeLogReposi
我正在尝试讲述一种方法 GetAll()在模拟对象上 _portalUserRepositoryMock返回 IQueryable 类型的对象.我知道是这个类型,因为要测试的类中的方法返回这个类型。
我正在尝试: ... int id = 5; //DB is a instance of DbContext MethodInfo methodFind = DB.GetType().GetMetho
我有以下在 EF7 之前使用的界面。当我尝试使用 EF7 构建应用程序时,我收到以下错误,而且我似乎无法在任何地方找到这种类型。我非常感谢您就如何为 EF7 更改此代码提出建议。 Error CS02
这是我不相信的代码。请检查我如何将实体集合作为参数传递。 public ExamProduced GetExamProduced(XElement xml) { var examProduce
我正在尝试测试存储库的 Get 方法。签名如下: public virtual IEnumerable Get( Expression> filter = null,
我是一名优秀的程序员,十分优秀!