我正在学习使用Azure移动应用程序中的C#编程。我创建了一个Model来连接我的Azure SQL数据库,创建了一个DataObject,如下所示:
public class Account : EntityData
{
//public int id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
public string Password { get; set; }
public DateTime dtCreated { get; set; }
public Guid oGuid { get; set; }
}
请注意,我在上面注释了public int id;这是因为查询时它会导致重复列错误。
最后,我使用新创建的 Account DataObject 创建了一个控制器。
所以我运行了应用程序并点击"tables/Account"函数,但它返回零行 (但有数据,我可以用在 Azure 移动应用中使用的用户进行查询)。
然后我注意到模型架构如下:
[
{
"id": 0,
"FirstName": "string",
"LastName": "string",
"PhoneNumber": "string",
"Password": "string",
"dtCreated": "2016-07-06T17:45:47.114Z",
"oGuid": "string",
"Id": "string",
"Version": "string",
"CreatedAt": "2016-07-06T17:45:47.114Z",
"UpdatedAt": "2016-07-06T17:45:47.114Z",
"Deleted": true
}
]
我看到配置的模型有几个问题(而且我不知道某些列来自哪里...)
首先,id被列两次,一次为int(必须是我的),另一个id为字符串,我不知道它来自哪里。
此外,在数据库中,oGuid的类型为uniqueIdentifier,而不是字符串。这可能是个问题,也可能不是,因为我还无法测试。
然后就是其他在我的数据库中不存在的列,包括CreatedAt(datetime)、UpdatedAt(datetime)、Version(string)和Deleted(bit)。
我认为我没有从那个调用中得到任何数据的原因/问题是数据不匹配。
我需要在api测试中添加在模型中列出的其他列吗?
我还尝试过调用/table/Account/3以加载特定的帐户,但没有返回任何行...我猜这是模型不匹配,但我不确定是不是这个问题或其他问题导致的?我没有看到任何错误或警告。
更新
我弄清楚了模型先和Azure之间发生了什么以及如何将现有的DB附加到新代码中。我要在这里发布这篇文章希望能为其他人节省时间。这应该更容易做到。我不喜欢CodeFirst(但),因为我喜欢手动控制DB...所以这使得我更容易处理db后端。
首先,我创建了一个新项目(Azure移动应用程序),然后在模型下右键单击模型并添加->新实体数据模型,然后添加Azure数据库名称、密码,并使用下面使用的"用户创建的配置文件名"。此连接必须在web.config中进行编辑,如下所示。
然后,我必须为DataObjects中的表创建模型(不包括MS所需的列),并从dataobject创建控制器。然后,我需要编辑web.config并设置非实体DB连接字符串:例如:
<add name="[user created preset name]" providerName="System.Data.SqlClient" connectionString="Server=[Azuredb server connection];initial catalog=[DBName];persist security info=True;user id=[user];password=[pass];MultipleActiveResultSets=True"/>
最后,在MobileServiceContext中,我需要将DataObject模型映射到Azure SQL表格,并将连接字符串从默认的MS_TableConnectionString设置为web.config中的连接字符串。
private const string connectionStringName = "Name=[user created preset name]";
在 OnModelCreating() 中,我添加了以下内容:
modelBuilder.Entity<Account>().ToTable("tblAccount");
Account是我在DataObjects中创建的模型(类),tblAccount是AzureDB中的表名。