EF4:ObjectSet.AddObject() 不起作用
我正在使用 Entity Framework 4 设置存储库,但无法使 ObjectSet.AddObject() 方法正常工作。这是我正在使用的代码 - 为了简单起见,我将其从存储库复制到我的单元测试方法中:
/* m_FilePath value is passed in by test initializer. */
// Configure a SQL CE connection string
var sqlCompactConnectionString = string.Format("Data Source={0}", m_FilePath);
// Create an Entity Connection String Builder
var builder = new EntityConnectionStringBuilder();
builder.Metadata = string.Format("res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", "Model.Notes");
builder.Provider = "System.Data.SqlServerCe.4.0";
builder.ProviderConnectionString = sqlCompactConnectionString;
var edmConnectionString = builder.ToString();
// Create object context and object set
var context = new NotesContainer(edmConnectionString);
var objectSet = context.CreateObjectSet<Note>();
// Add a note
var entity = new Note();
objectSet.AddObject(entity);
// Build assertion
var notes = objectSet.AsEnumerable();
var count = notes.Count();
Assert.AreEqual(1, count);
返回的计数为零 - 对象集为空,因此断言失败。当我单步执行代码时,会创建对象上下文和对象集,但对象集的枚举不会返回结果。
我的代码有什么错误?感谢您的帮助。
I am setting up a repository with Entity Framework 4, and I can't get the ObjectSet.AddObject() method working. Here is the code I am using--to keep things simple, I copied it out of the repository into my unit test method:
/* m_FilePath value is passed in by test initializer. */
// Configure a SQL CE connection string
var sqlCompactConnectionString = string.Format("Data Source={0}", m_FilePath);
// Create an Entity Connection String Builder
var builder = new EntityConnectionStringBuilder();
builder.Metadata = string.Format("res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", "Model.Notes");
builder.Provider = "System.Data.SqlServerCe.4.0";
builder.ProviderConnectionString = sqlCompactConnectionString;
var edmConnectionString = builder.ToString();
// Create object context and object set
var context = new NotesContainer(edmConnectionString);
var objectSet = context.CreateObjectSet<Note>();
// Add a note
var entity = new Note();
objectSet.AddObject(entity);
// Build assertion
var notes = objectSet.AsEnumerable();
var count = notes.Count();
Assert.AreEqual(1, count);
The count that is returned is zero--the object set is empty, so the assertion fails. When I step through the code, the object context and object set are created, but enumeration of the object set returns no results.
What's the error in my code? Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
notes.Count()
针对您的数据文件(我假设它是空的)执行。在调用context.SaveChanges()
之前,不会添加新对象。考虑:
添加新注释后添加
context.SaveChanges()
并且断言将通过。但是,当然,这会带来一系列全新的问题,即在每次测试运行中保持数据库状态相同。The
notes.Count()
is executed against your data file (which I assume is empty). Your new object won't be added until you callcontext.SaveChanges()
.Consider:
Add
context.SaveChanges()
after you add new Note and assert will pass. But of course, that introduces whole new range of problems with keeping DB state the same for every test run.这并不完全正确。
例如,父级/子级
当对 Parent 使用 Context.AddObject() 时,在调用 Context.SaveChanges() 之前不会添加它,但是添加子级时,将在不保存的情况下添加(当然设置外键时)。
不确定这是一个错误还是 EF 中的设计...
我目前看到的唯一“解决方法”:
同样调用Context.ParentSet.Execute(MergeOption.AppendOnly);并没有达到预期的效果!
解决方案/解决方法:我维护自己的对象列表,用于添加/删除 + 设置 DataSource = null,然后添加到我的内部列表。
由于需要设置DataSource = null,所以EF中似乎有一些奇怪的东西......
That is not entirely true.
e.g. Parent / Childs
When using Context.AddObject() for Parent it won't be added to until Context.SaveChanges() is called but when adding a Child it will be added without saving (when foreign keys are set of course).
Not sure if it is a bug or if it is by design in the EF ...
The only "workaround" I see at the moment:
Also calling Context.ParentSet.Execute(MergeOption.AppendOnly); does not have the expected effect!
Solution/Workaround: I maintain my own list of objects for adding/removing + setting the DataSource = null and then to my internal list.
Since it is necessary to set the DataSource = null it seems that there is something strange in EF ...