在azure

时间:2015-05-27 21:48:42

标签: c# sql asp.net linq azure

您好我在azure上发布了一个应用程序。当我尝试访问BookDetail页面时,出现InvalidColumn "Borrowed"错误。在具有PhoneNumber和Password列的UserDetail页面上也会发生同样的事情。

这是BookDetail getmethod

public IQueryable<Book> GetBook([QueryString("BookID")] int? BookID)
    {
        var _db = new WebApplication1.Models.LibraryContext();
        IQueryable<Book> query = _db.Books;
        if (BookID.HasValue && BookID > 0)
        {

            query = query.Where(p => p.BookID == BookID);
        }
        else
        {
            query = null;


        }

        if (query == null || query.Count() == 0)
        {


            inputUserBorrow.Visible = false;
            inputUserBorrowButton.Visible = false;

        }
        return query;
    }

这是我的模特

namespace WebApplication1.Models
{
public class LibUser
{

    [Key]
    public int UserID { get; set; }

    [Required, StringLength(50), Display(Name = "First Name")]
    public string UserFirstName { get; set; }

    [Required, StringLength(50), Display(Name = "Last Name")]
    public string UserLastName { get; set; }

    [Required, StringLength(100), Display(Name = "Street"), DataType(DataType.Text)]
    public string Adress { get; set; }

    [Required, StringLength(20), Display(Name = "Phone Number"), DataType(DataType.Text)]
    public string PhoneNumber { get; set; }

    public string Password { get; set; }




}

public class Book
{

    [Key]
    public int BookID { get; set; }

    public string Title { get; set; }

    public string Author { get; set; }

    public DateTime Published { get; set; }

    public bool Borrowed { get; set; }

    public Book() {
        Borrowed = false;
    }
}



public class Borrowed
{
    [Key]
    public int BorrowID { get; set; }

    public int UserID { get; set; }

    public int BookID { get; set; }

    public string BookTitle { get; set; }

    public DateTime Due { get; set; }

}

}

这是我的上下文文件

namespace WebApplication1.Models
{
public class LibraryContext : DbContext
{

    public LibraryContext()
        : base("LibraryContext")
    {
    }

    public DbSet<LibUser> LibUsers { get; set; }
    public DbSet<Book> Books { get; set; }
    public DbSet<Borrowed> Borrows { get; set; }

}
}

堆栈跟踪

[SqlException (0x80131904): Invalid column name 'Borrowed'.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1787814
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5341674
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +546
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1693
   System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +61
   System.Data.SqlClient.SqlDataReader.get_MetaData() +90
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +377
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) +1421
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +177
  System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +137
   System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +41
   System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +10
   System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__c(DbCommand t, DbCommandInterceptionContext`1 c) +9
       System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) +72              System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext) +356
      System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior) +166
   System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +10
   System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) +37

当我将我的应用程序发布到azure并在azure服务上使用新数据库时出现此问题。 感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

今天下午我遇到了同样的问题。花了大约2个小时的重新部署并尝试新的迁移。关闭和打开应用程序服务对我来说是个窍门。

enter image description here

答案 1 :(得分:0)

在Azure门户的“ Web App应用程序设置”页面上检查连接字符串。

这些覆盖的连接在web.config中指定。

Azure portal screenshot of connection strings field in web app application settings

我自己也遇到了同样的问题。

在本地运行,连接到Azure上的db,一切正常(使用web.config中的连接字符串)。

发布到Azure应用程序服务无效。 web.config中的相同连接字符串相同,但使用的是门户网站中指定的连接字符串,将应用程序指向数据库的最新版本。

在门户网站中更新这些连接字符串可解决此问题。