سؤال

Like the title says, I am trying to connect a new MVC 5 app created by Visual Studio 2013 to an existing database table created by and Azure Mobile Service.

هل كانت مفيدة؟

المحلول

I realized that the issue was with the schema of the tables that were created by the mobile service. Instead of using the default "dbo" schema, the tables used <mobile_service_name> as the schema. My MVC project was looking for dbo.<Table_Name> instead of <mobile_service_name>.<Table_Name> and was throwing an error as a result. To fix this, you need to add some mappings in you DB Context class to tell it where exactly to find the tables it is looking for. These mappings are done in an overridden method called OnModelCreating. It ends up looking like this:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext() : base("DefaultConnection")
    {
    }

    public System.Data.Entity.DbSet<MyObject> MyObjects { get; set; }

    protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<MyObject>().ToTable("<mobile_service_name>.<Table_Name>");
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top