我使用 Code First 在本地服务器上成功创建了数据库,并将其迁移到 Azure,现在我想使用 Azure 连接字符串和我现有的 POCO 类连接到该数据库。
我的连接字符串:
<add name="CijenolomciContext" connectionString="Data Source=*.database.windows.net;Initial Catalog=NewCijenolomci;Persist Security Info=True;User ID=Bip*;Password=**;MultipleActiveResultSets=False"" providerName="System.Data.EntityClient" />
但是这部分代码恰好在服务器上没有执行任何操作:
Item i = new Item();
i.Name = "Cranberry";
i.OldPrice = new decimal(25.99);
i.NewPrice = new decimal(14.99);
i.SaleStarts = DateTime.Parse("2012-01-21");
i.CategoryID = 1;
context.Items.Add(i);
context.SaveChanges();
如何成功添加?
编辑: 这种方法恰好修改了我的本地数据库,而不是服务器数据库。
最佳答案
对我来说,您似乎没有连接到 Azure 数据库。我希望连接字符串更像这样。
Server=tcp:[serverName].database.windows.net;Database=myDataBase;User ID=[LoginForDb]@[serverName];Password=myPassword;Trusted_Connection=False;Encrypt=True;
Use 'username@servername' for the User ID parameter.
关于.net - EF 代码首先存在 SQL Azure 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9839313/