问题
I have an Azure Mobile Service that works perfectly locally and has been thoroughly tested using Fiddler. I setup Reference Handling to avoid self referencing loops
WebApiConfig.cs
public static class WebApiConfig
{
public static void Register()
{
// Use this class to set configuration options for your mobile service
ConfigOptions options = new ConfigOptions();
// Use this class to set WebAPI configuration options
HttpConfiguration config = ServiceConfig.Initialize(new ConfigBuilder(options));
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
Formatting = Newtonsoft.Json.Formatting.Indented,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
// To display errors in the browser during development, uncomment the following
// line. Comment it out again when you deploy your service for production use.
// config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
Database.SetInitializer(new MobileServiceInitializer());
}
My context is pretty simple
public class SAServiceContext : DbContext
{
private const string connectionStringName = "Name=MS_TableConnectionString";
public SAServiceContext()
: base(connectionStringName)
{
}
public DbSet<Recipe> Recipes { get; set; }
public DbSet<Method> Methods { get; set; }
public DbSet<RecipeItem> Items { get; set; }
public DbSet<DietType> DietTypes { get; set; }...
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
string schema = ServiceSettingsDictionary.GetSchemaName();
if (!string.IsNullOrEmpty(schema))
{
modelBuilder.HasDefaultSchema(schema);
}
modelBuilder.Conventions.Add(
new AttributeToColumnAnnotationConvention<TableColumnAttribute, string>(
"ServiceTableColumn", (property, attributes) => attributes.Single().ColumnType.ToString()));
}
}
As I said this all works perfectly locally and when I publish the service to the cloud I can see the MS_TableConnectionString is ok but the service does not work. It starts up ok and I can see all the relevant methods from my TableControllers but as soon as I click one (eg GETSeason) I get the generic error
An exception has occurred while using the formatter 'JsonMediaTypeFormatter' to generate sample for media type 'application/json'. Exception message: One or more errors occurred.
I have tried
PreserveReferencesHandling = PreserveReferencesHandling.Objects
but to no avail. Also noted is that the tables are not being created in SQL Azure as it never gets to the Database Initilaizer method called from WebApiConfig. The logs in MS Azure portal arent showing any errors either. Im stuck any help appreciated
EDIT Now seeing the following error in Azure portal. MS_TableConnectionString is auto created in Azure so not sure why its saying thats a problem. Locally I just use connectionString in web.config
Exception=System.InvalidOperationException: Database initialization failed. Could not initialize one or more objects in schema 'smallacorns_v3'. Please ensure that the database connection string is correct. For more details on the error, please see the inner exception. ---> System.InvalidOperationException: Database initialization failed. Could not initialize one or more objects in schema 'smallacorns_v3'. Please ensure that the database connection string is correct. For more details on the error, please see the inner exception. ---> System.Data.SqlClient.SqlException: User does not have permission to perform this action.
回答1:
This looks like the user using which you are connecting to the Azure SQL Database doesn't have permissions to create the Azure SQL database.
Try giving the database user, 'db_owner' role and this error should go away.
来源:https://stackoverflow.com/questions/27012980/publishing-azure-mobile-service-doesnt-work