问题描述
因此,我创建了一个Azure移动服务,下载了一个项目并运行它.首次启动后,我在数据库中看到一些新表:TodoItems
(有2个项目)和_MigrationHistory
.到目前为止一切顺利.
So, i create a azure mobile service, downloaded a project and run it.After the first launch, i see some new tables in DB: TodoItems
(with 2 items) and _MigrationHistory
. So far so good.
现在,我想添加一个额外的表.
Now, i'd like to add an extra table.
- 我正在制作新模型
MyModel:EntityData { public string MyData { get; set; }
- 制作一个新的控制器
MyModelController:TableController<MyModel>
,它是TodoItemController的副本,其中TodoItem
被替换为MyModel
- 将
public DbSet<MyModel> MyModels { get; set; }
添加到MyProjectContext.cs - 在WebApiConfig中添加一行
context.Set<MyModel>().Add(new MyModel{MyData= "test"});
- I'm making new model
MyModel:EntityData { public string MyData { get; set; }
- Making a new controller
MyModelController:TableController<MyModel>
which is a copy of TodoItemController, whereTodoItem
is replaced withMyModel
- Added
public DbSet<MyModel> MyModels { get; set; }
to MyProjectContext.cs - Adding to the WebApiConfig a line
context.Set<MyModel>().Add(new MyModel{MyData= "test"});
我忘记了什么吗?
删除所有表并重新发布并运行我的移动应用程序后,我遇到 MobileServiceInvalidOperationException 异常,并且我的表为空(甚至TodoItems
为空).
After deleting all tables and re-publishing and running my mobile application, i'm getting MobileServiceInvalidOperationException exception and my tables are empty (even TodoItems
is empty).
编辑:我不确定问题的根源在哪里.如果要向移动应用添加一行
EDIT: i'm not sure where is the core of the problem. If to add to the mobile app a line
private IMobileServiceTable<MyModel> myItemsTable = App.MobileService.GetTable<MyModel>();
它将引发InvalidOperationException.但是MyModels
表存在于数据库中,我可以通过数据库管理器看到它...
It would throw InvalidOperationException. But MyModels
table exists in a db, i can see it via db manager...
EDIT2 :
- 好的,我删除了所有其他代码(使用MyModels)并删除了db中的所有表.重新发布移动服务,运行应用程序,然后再次看到2个表:
TodoItems
(有2个项目)和_MigrationHistory
. - 现在,我在MyProjectContext.cs中添加了行
public DbSet<MyModel> MyModels { get; set; }
,删除了db中的所有表,运行了app,现在我看到了3个表:MyModels
,TodoItems
(有2个项目)和_MigrationHistory
. - 当我添加行
context.Set<MyModel>().Add(new MyModel{MyData= "test"});
时-此后播种出错:我看到3个表MyModels
,TodoItems
和_MigrationHistory
,但是TodoItems
现在为空.
- Okay, i removed all additional code (with MyModels) and removed all tables in db. Re-publish mobile service, run app and again i see 2 tables:
TodoItems
(with 2 items) and_MigrationHistory
. - Now, i added line
public DbSet<MyModel> MyModels { get; set; }
to MyProjectContext.cs, removed all tables in db, run app and now i see 3 tables:MyModels
,TodoItems
(with 2 items) and_MigrationHistory
. - When i added line
context.Set<MyModel>().Add(new MyModel{MyData= "test"});
- after this moment seeding went wrong: i see 3 tablesMyModels
,TodoItems
and_MigrationHistory
, butTodoItems
is empty now.
推荐答案
问题出在context.Set<MyModel>().Add(new MyModel{MyData= "test"});
行中.我忘了初始化Id
:认为它会自动递增.
The problem was in line context.Set<MyModel>().Add(new MyModel{MyData= "test"});
. I forgot to initialize Id
: thought it would be autoincremented.
固定行是context.Set<MyModel>().Add(new MyModel{MyData= "test", Id = "666"});
这是我面临的另一个问题:如果应用程序的var myTable = MobileService.GetTable<MyModel>();
行中有例外,请确保已将Id
字段添加到模型中.
just another problem i faced: if there's exception in app's line var myTable = MobileService.GetTable<MyModel>();
, be sure you added Id
field to your model.
这篇关于扩展基本的移动天蓝色样本(.net后端)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!