【问题标题】:EF Code first existing SQL Azure DatabaseEF Code 首先是现有的 SQL Azure 数据库
【发布时间】:2012-04-08 00:28:24
【问题描述】:

我使用 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&quot;" 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();

如何添加成功?

编辑: 这种方法恰好修改了我的本地数据库,而不是服务器。

【问题讨论】:

    标签: .net sql entity-framework azure ef-code-first


    【解决方案1】:

    好的。这里是。在 DbContext 中,我继承了基本构造函数:

     public CijenolomciContext()
            : base(*database name*)
        {
            //additional logic
        }
    

    尽管我在App.config 中添加了我的连接字符串,但他始终连接到本地。

    我对连接字符串进行了硬编码,然后将其添加到构造函数中。现在它起作用了! :)

    【讨论】:

      【解决方案2】:

      对我来说,您似乎没有连接到 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.
      

      Connection Strings for SQL Azure

      【讨论】:

      • 我使用这种连接字符串并不断添加到本地数据库:/
      • 这可能是因为我继承了 DbContext 中的基本构造函数,但现在当我更改它时,我得到 System.ArgumentException: Keyword not supported: 'server'.
      猜你喜欢
      • 2015-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-18
      • 1970-01-01
      • 2014-08-05
      相关资源
      最近更新 更多