如何制作实体框架代码首先使用SQL Azure DB?

Eth*_*nHY 4 c# entity-framework azure ef-code-first azure-sql-database

我现在开始学习EF了.我想要做的是使用EF代码首先在SQL Azure中创建新表.

这是我的代码:

namespace WebApplication2
{
   public class Blog
   {
       public int UserName { get; set; }
       public int ID { get; set; }
   }

   public class BlogContext : DbContext
   {
       public DbSet<Blog> Blogs { get; set; }

       public BlogContext() : base("MYEF")
       {
       }
   }
}
Run Code Online (Sandbox Code Playgroud)

代码背后:

 protected void Page_Load(object sender, EventArgs e)
 {
        using (var db = new BlogContext())
        {
            var blog = new Blog { UserName = 456 };
            db.Blogs.Add(blog);
            db.SaveChanges();
        }
    }
Run Code Online (Sandbox Code Playgroud)

Web.config:

<connectionStrings>
    <add name="TestDBContext" 
         connectionString="Server=tcp:mydb.database.windows.net,1433;Database=MYEF;User ID=[Username];Password=[MyPassword];Trusted_Connection=False;Encrypt=True;PersistSecurityInfo=True" 
         providerName="System.Data.SqlClient" />
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)

当我检查我的SQL Azure DB时,没有创建任何表,EF仍然MYEF在默认的SQL Server Express数据库中创建表.

有人做过这个吗?

Pas*_*cal 13

您应该将连接字符串名称传递给BlogContext此类的基本构造函数

public BlogContext()
    : base("TestDBContext")
{
}
Run Code Online (Sandbox Code Playgroud)

http://msdn.microsoft.com/en-us/library/gg679467(v=vs.103).aspx