Extending base mobile azure sample (.net backend)

一世执手 提交于 2019-12-20 02:10:52

问题


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.

  1. I'm making new model MyModel:EntityData { public string MyData { get; set; }
  2. Making a new controller MyModelController:TableController<MyModel> which is a copy of TodoItemController, where TodoItem is replaced with MyModel
  3. Added public DbSet<MyModel> MyModels { get; set; } to MyProjectContext.cs
  4. Adding to the WebApiConfig a line 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).

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. But MyModels table exists in a db, i can see it via db manager...

EDIT2:

  • 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 tables MyModels, TodoItems and _MigrationHistory, but TodoItems is empty now.

回答1:


The problem was in line context.Set<MyModel>().Add(new MyModel{MyData= "test"});. I forgot to initialize Id: thought it would be autoincremented.

Fixed line is 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>();, be sure you added Id field to your model.




回答2:


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.

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:

    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:

    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:

public class MyInitializer : ClearDatabaseSchemaAlways<todoitemContext>

But from then on you can just use the ClearDatabaseSchemaAIfModelChanges again.

Hope this helps!

Henrik



来源:https://stackoverflow.com/questions/25016543/extending-base-mobile-azure-sample-net-backend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!