简体   繁体   English

在本地计算机中使用Azure移动服务进行开发[.Net后端]

[英]Developing with Azure Mobile Services in a local computer[.Net backend]

I'm developing a cloud service using Azure Mobile Services, and in order to test it quickly and debug it, I want to deploy it in my local computer. 我正在使用Azure移动服务开发云服务,为了快速测试并调试它,我想在本地计算机中部署它。 I select the project, hit F5, got it running in IIS express in my local PC. 我选择了项目,点击F5,在我的本地PC上运行IIS Express。 I execute the cliente against my local URI address of the IIS service and when I try to insert a value, this exception appears if I try to retrieve or insert a new object: 我针对IIS服务的本地URI地址执行客户端,当我尝试插入值时,如果我尝试检索或插入新对象,则会出现此异常:

An exception of type 'System.ArgumentException' occurred in EntityFramework.SqlServer.dll but was not handled in user code 

Additional information: The database name 'TR_MyTrip_Server_ExtraData_Service]_Persons_InsertUpdateDelete' is invalid. Database names must be of the form [<schema_name>.]<object_name>.

I debugged the Initialization of the controller and found out that mobile services deploys a LocalDb instance with this connection info on the DataBase property of the ServiceContext object: 我调试了控制器的初始化,发现移动服务在ServiceContext对象的DataBase属性上使用此连接信息部署了一个LocalDb实例:

Data Source=(localdb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MyTrip.Server.ExtraData.Service-20140731060708.mdf;Initial Catalog=aspnet-TestProject.Server.ExtraData.Service-20140731060708;Integrated Security=True;MultipleActiveResultSets=True

It is not able to find this DataBase if I try to connect with this connection string to the LocalDB via the SQL Management Studio 如果我尝试通过SQL Management Studio将此连接字符串连接到LocalDB,则无法找到此DataBase

I'm able to access to the LocalDb instance with the SQL management studio running this command on CMD and retrieving the Connection for the LocalDB 我可以使用在CMD上运行此命令的SQL管理工作室访问LocalDb实例并检索LocalDB的连接

SqlLocalDB.exe info v11.0

Eitherway, on the SQL Management Studio I'm not able to see any DataBase relating my controller of the Mobile Services. 无论如何,在SQL Management Studio上,我无法看到任何与我的移动服务控制器相关的数据库。 I searched on google and the only related link I found was this but is not working... Does anybody know what's happening? 我搜索谷歌和我发现的唯一相关链接是这个,但没有工作...有谁知道发生了什么?

Thank you so much 非常感谢

This is a problem when you have a project name with periods in it. 如果项目名称中包含句点,则会出现此问题。 Like TestProject.Server.ExtraData.Service. 与TestProject.Server.ExtraData.Service类似。

To fix this, go into the web.config and edit the appsetting named "MS_MobileServiceName" taking out the periods. 要解决此问题,请进入web.config并编辑名为“MS_MobileServiceName”的appsetting,取出句点。 This is the value used as the SQL schema name for your mobile service tables in the database. 这是用作数据库中移动服务表的SQL模式名称的值。 It can't have periods or other special characters in it. 它不能包含句号或其他特殊字符。

I generally try to make this the same as the name of the mobile service I'll deploy to, but it's not technically required. 我通常会尝试将其与我将部署的移动服务的名称相同,但这在技术上并不需要。

For whatever reason, I couldn't get this working using the accepted answer. 无论出于何种原因,我都无法使用已接受的答案来解决这个问题。 I assume this has something to do with the disclaimer in web.config which states: 我认为这与web.config中的免责声明有关,该声明表明:

After publishing to Mobile Services, these settings will be overridden by the values specified in the portal. 发布到移动服务后,这些设置将被门户网站中指定的值覆盖。

However, I did find another work around in the MobileServiceContext file where I replaced this line: 但是,我确实在MobileServiceContext文件中找到了另一个解决方法,我替换了这一行:

string schema = ServiceSettingsDictionary.GetSchemaName();
if (!string.IsNullOrEmpty(schema))
{
    modelBuilder.HasDefaultSchema(schema);
}

with a simple string value modelBuilder.HasDefaultSchema('mySchema'); 使用简单的字符串值modelBuilder.HasDefaultSchema('mySchema');

I imagine there's a way to influence the output of GetSchemaName() but, considering how much effort it took to resolve this problem, I am perfectly content with this solution for now. 我想有一种方法可以影响GetSchemaName()的输出,但是,考虑到解决这个问题花了多少精力,我现在非常满意这个解决方案。

If you've changed the MS_MobileServiceName in config to remove the periods, and you're still getting the error, you'll need to re-run the scaffolding for the Initial migration in the package manager console: 如果您已更改config中的MS_MobileServiceName以删除句点,并且您仍然收到错误,则需要在程序包管理器控制台中重新运行初始迁移的脚手架:

Add-Migration Initial -Force Add-Migration Initial -Force

From what I could see, the scaffolding generates an embedded .resx that still has your old schema name (DefaultSchema), and a snapshot (Target) that is referenced in the designer code. 从我所看到的,脚手架生成一个嵌入的.resx,它仍然具有旧的模式名称(DefaultSchema),以及设计器代码中引用的快照(Target)。 Just changing the DefaultSchema didn't solve the issue for me, but re-running the scaffolding did. 只是更改DefaultSchema并没有解决我的问题,但重新运行脚手架。

I also added Table annotations to my models, but I don't think that was the issue. 我还在我的模型中添加了表注释,但我不认为这是问题所在。 And probably isn't ideal if your service is already live. 如果您的服务已经存在,可能并不理想。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM