我有简单的Azure Mobile Service
,它与MySql Database
Web.Config
<connectionStrings>
<add name="MS_TableConnectionString" connectionString="Server=11.11.111.111;User Id=test;Password=test;Database=testDB" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
App_Start\WebApiConfig.cs
public static class WebApiConfig
{
public static void Register()
{
ConfigOptions options = new ConfigOptions();
HttpConfiguration config = ServiceConfig.Initialize(new ConfigBuilder(options));
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
}
}
Controllers\CommentController.cs
public class CommentController : TableController<wp_comments>
{
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
MobileServiceContext context = new MobileServiceContext();
DomainManager = new EntityDomainManager<wp_comments>(context, Request, Services);
}
// GET tables/Comment
public IQueryable<wp_comments> GetAllComment()
{
return Query(); // Getting Exception Here
}
DataObjects\wp_comments.cs
public class wp_comments : EntityData
{
public int comment_ID { get; set; }
public int comment_post_ID { get; set; }
[Column("comment_author", TypeName = "NVARCHAR(MAX)")]
public string comment_author { get; set; }
[Column("comment_author_email", TypeName = "NVARCHAR(MAX)")]
public string comment_author_email { get; set; }
[Column("comment_author_url", TypeName = "NVARCHAR(MAX)")]
public string comment_author_url { get; set; }
[Column("comment_author_IP", TypeName = "NVARCHAR(MAX)")]
public string comment_author_IP { get; set; }
public DateTime comment_date { get; set; }
public DateTime comment_date_gmt { get; set; }
[Column("comment_author_IP", TypeName = "NVARCHAR(MAX)")]
public string comment_content { get; set; }
public int comment_karma { get; set; }
[Column("comment_approved", TypeName = "NVARCHAR(MAX)")]
public string comment_approved { get; set; }
[Column("comment_agent", TypeName = "NVARCHAR(MAX)")]
public string comment_agent { get; set; }
[Column("comment_type", TypeName = "NVARCHAR(MAX)")]
public string comment_type { get; set; }
public int comment_parent { get; set; }
public int user_id { get; set; }
}
Models\MobileServiceContext.cs
public class MobileServiceContext : DbContext
{
private const string connectionStringName = "Name=MS_TableConnectionString";
public MobileServiceContext() : base(connectionStringName)
{
}
public DbSet<wp_comments> Comments { 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()));
}
}
答案 0 :(得分:1)
请检查您的型号,您已定义相同的属性&#34; comment_author_IP&#34;。请修改它,然后查看您的代码是否有效。
[Column("comment_author_IP", TypeName = "NVARCHAR(MAX)")]
public string comment_author_IP { get; set; }
public DateTime comment_date { get; set; }
public DateTime comment_date_gmt { get; set; }
[Column("comment_author_IP", TypeName = "NVARCHAR(MAX)")]
public string comment_content { get; set; }