[英]Extending base mobile azure sample (.net backend)
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
. 第一次启动后,我在DB中看到一些新表:
TodoItems
(有2个项目)和_MigrationHistory
。 So far so good. 到现在为止还挺好。
Now, i'd like to add an extra table. 现在,我想添加一个额外的表格。
MyModel:EntityData { public string MyData { get; set; }
MyModel:EntityData { public string MyData { get; set; }
MyModel:EntityData { public string MyData { get; set; }
MyModelController:TableController<MyModel>
which is a copy of TodoItemController, where TodoItem
is replaced with MyModel
MyModelController:TableController<MyModel>
其是其中TodoItemController,副本TodoItem
被替换MyModel
public DbSet<MyModel> MyModels { get; set; }
public DbSet<MyModel> MyModels { get; set; }
public DbSet<MyModel> MyModels { get; set; }
public DbSet<MyModel> MyModels { get; set; }
to MyProjectContext.cs public DbSet<MyModel> MyModels { get; set; }
到MyProjectContext.cs context.Set<MyModel>().Add(new MyModel{MyData= "test"});
context.Set<MyModel>().Add(new MyModel{MyData= "test"});
Do i forget something? 我忘了什么吗?
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). 删除所有表并重新发布并运行我的移动应用程序后,我收到MobileServiceInvalidOperationException异常,我的表为空(即使
TodoItems
为空)。
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>();
It would throw InvalidOperationException. 它会抛出InvalidOperationException。 But
MyModels
table exists in a db, i can see it via db manager... 但
MyModels
表存在于db中,我可以通过db manager看到它...
EDIT2 : 编辑2 :
TodoItems
(with 2 items) and _MigrationHistory
. TodoItems
(有2个项目)和_MigrationHistory
。 public DbSet<MyModel> MyModels { get; set; }
public DbSet<MyModel> MyModels { get; set; }
public DbSet<MyModel> MyModels { get; set; }
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
. public DbSet<MyModel> MyModels { get; set; }
到MyProjectContext.cs,除去分贝的所有表中,运行的应用程序和现在我看到3个表: MyModels
, TodoItems
(具有2项)和_MigrationHistory
。 context.Set<MyModel>().Add(new MyModel{MyData= "test"});
context.Set<MyModel>().Add(new MyModel{MyData= "test"});
- after this moment seeding went wrong: i see 3 tables MyModels
, TodoItems
and _MigrationHistory
, but TodoItems
is empty now. MyModels
, TodoItems
和_MigrationHistory
,但TodoItems
现在是空的。 The problem was in line context.Set<MyModel>().Add(new MyModel{MyData= "test"});
问题出在行
context.Set<MyModel>().Add(new MyModel{MyData= "test"});
. 。 I forgot to initialize
Id
: thought it would be autoincremented. 我忘了初始化
Id
:认为它会自动增加。
Fixed line is context.Set<MyModel>().Add(new MyModel{MyData= "test", Id = "666"});
固定行是
context.Set<MyModel>().Add(new MyModel{MyData= "test", Id = "666"});
EDIT1: just another problem i faced: if there's exception in app's line var myTable = MobileService.GetTable<MyModel>();
EDIT1:我遇到的另一个问题:如果app的行中有异常,则
var myTable = MobileService.GetTable<MyModel>();
, be sure you added Id
field to your model. ,请务必将
Id
字段添加到模型中。
It's a bit more tricky than it should be but the reason for the error is that the initializer doesn't see that the MyModel table isn't there -- only that the TodoItem model hasn't changed. 这比它应该有点棘手,但错误的原因是初始化程序没有看到MyModel表不存在 - 只是TodoItem模型没有改变。
The easiest workaround is that you just add to your MyModel DbSet to existing todoitem context instead of adding a new MyModelContext so that it looks like this: 最简单的解决方法是,只需将MyModel DbSet添加到现有的todoitem上下文,而不是添加新的MyModelContext,使其如下所示:
public DbSet<TodoItem> TodoItems { get; set; }
public DbSet<MyModel> MyModels { get; set; }
Then you can also just have one DB initializer, for example looking like this: 然后你也可以只有一个DB初始化程序,例如看起来像这样:
protected override void Seed(henrikntest13Context context)
{
List<TodoItem> todoItems = new List<TodoItem>
{
new TodoItem { Id = "1", Text = "First item", Complete = false },
new TodoItem { Id = "2", Text = "Second item", Complete = false },
};
foreach (TodoItem todoItem in todoItems)
{
context.Set<TodoItem>().Add(todoItem);
}
List<MyModel> myModels = new List<MyModel>
{
new MyModel { Id = "1", MyData = "First Item"},
new MyModel { Id = "2", MyData = "Second Item"},
};
foreach (MyModel model in myModels)
{
context.Set<MyModel>().Add(model);
}
base.Seed(context);
}
The first time you run this you may have to use the ClearDatabaseSchemaAlways as baseclass to force the model to get updated with the two entities: 第一次运行它时,您可能必须使用ClearDatabaseSchemaAlways作为基类来强制模型使用两个实体进行更新:
public class MyInitializer : ClearDatabaseSchemaAlways<todoitemContext>
But from then on you can just use the ClearDatabaseSchemaAIfModelChanges again. 但从那时起,您可以再次使用ClearDatabaseSchemaAIfModelChanges。
Hope this helps! 希望这可以帮助!
Henrik 亨里克
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.