EF4:ObjectSet.AddObject() 不起作用

发布于 2024-11-02 23:04:40 字数 1103 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情洁癖 2024-11-09 23:04:40

notes.Count() 针对您的数据文件(我假设它是空的)执行。在调用 context.SaveChanges() 之前,不会添加新对象。

考虑:

objectSet.AddObject(new Note()); // new object present in memory
var notes = objectSet.AsEnumerable();
var count = notes.Count();       // query against DB file; 
                                 // what's in memory at this point is irrelevant

添加新注释后添加 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 call context.SaveChanges().

Consider:

objectSet.AddObject(new Note()); // new object present in memory
var notes = objectSet.AsEnumerable();
var count = notes.Count();       // query against DB file; 
                                 // what's in memory at this point is irrelevant

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.

疏忽 2024-11-09 23:04:40

这并不完全正确。

例如,父级/子级

当对 Parent 使用 Context.AddObject() 时,在调用 Context.SaveChanges() 之前不会添加它,但是添加子级时,将在不保存的情况下添加当然设置外键时)。

不确定这是一个错误还是 EF 中的设计...

我目前看到的唯一“解决方法”:

  • 的引用
  • 引入一个 Client 表,每个对象(Parent、Child)都有对 Client调用上下文 .SaveChanges() (缺点:用户没有机会取消操作)

同样调用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:

  • introduce a Client table and each object (Parent, Child) has a reference to a Client
  • call context.SaveChanges() (Drawback: the user has no chance to cancel the operation)

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 ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文