如何解决SQL在代码优先迁移部署Azure期间无法找到数据类型BLOB的问题

huangapple go评论77阅读模式
英文:

How to solve SQL Cannot find data type BLOB during code-first migration to deploy Azure

问题

我想用Azure SQL替换我目前在ASP.NET Core上使用的SQLite数据库。我已生成资源,定义了项目上的SQL Server等等。

当我想将应用程序发布到Azure的部署服务器和云中的SQL数据库时,它显示“找不到数据类型BLOB”。我查看了互联网上的解决方案,但没有起作用。我猜我必须更新我的模型,但我不知道该如何做?

这里首先我将useSqlLiteServer更改为UseSqlServer

services.AddDbContext<DataContext>(x => x.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

在我的模型下面,我还需要更改什么,以下是我的一个模型,其他模型也类似。

namespace HospitalAppointmentAPI.Models
{
    public class Patient
    {   
        public int Id { get; set; }
        public string IdentityNumber { get; set; }
        public string Name { get; set; } 
        public byte[] PasswordHash { get; set; }
        public byte[] PasswordSalt { get; set; }
        public DateTime BirthDate { get; set; }
        public string Email { get; set; }
        public ICollection<Appointment> Appointments { get; set; }
    }
}

DataContext.cs

public class DataContext : DbContext
{
    public DataContext(DbContextOptions<DataContext> options) : base(options) {}
    public DbSet<Hospital> Hospitals { get; set; }
    public DbSet<Department> Departments { get; set; }
    public DbSet<Doctor> Doctors { get; set; }
    public DbSet<Patient> Patients { get; set; }
    public DbSet<Appointment> Appointments { get; set; }
}

在EF迁移脚本生成期间的完整错误消息

C:\Program Files\dotnet\sdk.0.100\Sdks\Microsoft.NET.Sdk.Publish\targets\PublishTargets\Microsoft.NET.Sdk.Publish.MSDeploy.targets(140,5):
Error : Web deployment task failed. (An error occurred during
execution of the database script. The error occurred between the
following lines of the script: "23" and "39". The verbose log might
have more information about the error. The command started with the
following:
"IF NOT EXISTS(SELECT * FROM [__EFMigrationsHisto"
Column, parameter, or variable #4: Cannot find data type BLOB. http://go.microsoft.com/fwlink/?LinkId=178587  Learn more at:
http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_SQL_EXECUTION_FAILURE.)
Failed to publish the database. This can happen if the remote database cannot run the script. Try modifying the database scripts, or
disabling database publishing in the Package/Publish Web properties
page. If the script failed due to database tables already exist, try
dropping existing database objects before creating new ones. For more
information on doing these options from Visual Studio, see
http://go.microsoft.com/fwlink/?LinkId=179181.
Error details:
An error occurred during execution of the database script. The error occurred between the following lines of the script: "23" and
"39". The verbose log might have more information about the error. The
command started with the following:
"IF NOT EXISTS(SELECT * FROM [__EFMigrationsHisto"
Column, parameter, or variable #4: Cannot find data type BLOB.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject
stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream,
BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject
stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String
methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1
completion, String methodName, Boolean sendToPipe, Int32 timeout,
Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at Microsoft.Web.Deployment.DBStatementInfo.Execute(DbConnection
connection, DbTransaction transaction, DeploymentBaseContext
baseContext, Int32 timeout)
Publish failed to deploy.

希望这能帮助您解决问题。如果需要更多帮助,请告诉我。

英文:

I want to exchange the currently sqlite database that I'm using on ASP.NET Core with Azure SQL.I generated resource,sql server everything defined on project.

When I want to publish the app to deployment server of azure and sql database in cloud,it says Cannot find data type BLOB .I checked solutions on the internet but didn't work.I guess I have to update my model but I don't know in what way?

Here at first I've changed useSqlLiteServer to UseSqlServer

  services.AddDbContext&lt;DataContext&gt;(x =&gt; x.UseSqlServer(Configuration.GetConnectionString(&quot;DefaultConnection&quot;)));

What else should I change on my models below listed my one model below others are similiar as well

namespace HospitalAppointmentAPI.Models
{
    public class Patient
    {	
		public int Id { get; set; }
        public string IdentityNumber { get; set; }
        public string Name { get; set; } 
        public byte[] PasswordHash { get; set; }
        public byte[] PasswordSalt { get; set; }
        public DateTime BirthDate { get; set; }
		public string Email { get; set; }
        public ICollection&lt;Appointment&gt; Appointments { get; set; }

    }
}

DataContext.cs

 public class DataContext : DbContext
    {
        public DataContext(DbContextOptions&lt;DataContext&gt; options) : base(options) {}
        public DbSet&lt;Hospital&gt; Hospitals { get; set; }
        public DbSet&lt;Department&gt; Departments { get; set; }
        public DbSet&lt;Doctor&gt; Doctors { get; set; }
        public DbSet&lt;Patient&gt; Patients { get; set; }
        public DbSet&lt;Appointment&gt; Appointments { get; set; }
       
    }

Full Error Message during EF migrations script generation

> C:\Program
> Files\dotnet\sdk\3.0.100\Sdks\Microsoft.NET.Sdk.Publish\targets\PublishTargets\Microsoft.NET.Sdk.Publish.MSDeploy.targets(140,5):
> Error : Web deployment task failed. (An error occurred during
> execution of the database script. The error occurred between the
> following lines of the script: "23" and "39". The verbose log might
> have more information about the error. The command started with the
> following:
> "IF NOT EXISTS(SELECT * FROM [__EFMigrationsHisto"
> Column, parameter, or variable #4: Cannot find data type BLOB. http://go.microsoft.com/fwlink/?LinkId=178587 Learn more at:
> http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_SQL_EXECUTION_FAILURE.)
> Failed to publish the database. This can happen if the remote database cannot run the script. Try modifying the database scripts, or
> disabling database publishing in the Package/Publish Web properties
> page. If the script failed due to database tables already exist, try
> dropping existing database objects before creating new ones. For more
> information on doing these options from Visual Studio, see
> http://go.microsoft.com/fwlink/?LinkId=179181.
> Error details:
> An error occurred during execution of the database script. The error occurred between the following lines of the script: "23" and
> "39". The verbose log might have more information about the error. The
> command started with the following:
> "IF NOT EXISTS(SELECT * FROM [__EFMigrationsHisto"
> Column, parameter, or variable #4: Cannot find data type BLOB. http://go.microsoft.com/fwlink/?LinkId=178587 Learn more at:
> http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_SQL_EXECUTION_FAILURE.
> Column, parameter, or variable #4: Cannot find data type BLOB.
> at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)
&gt; at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
&gt; exception, Boolean breakConnection, Action
1 wrapCloseInAction)
> at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject
> stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
> at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream,
> BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject
> stateObj, Boolean& dataReady)
> at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String
> methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
> at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1
> completion, String methodName, Boolean sendToPipe, Int32 timeout,
> Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
> at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
> at Microsoft.Web.Deployment.DBStatementInfo.Execute(DbConnection
> connection, DbTransaction transaction, DeploymentBaseContext
> baseContext, Int32 timeout)
> Publish failed to deploy.

答案1

得分: 5

在Sqlite中,BLOB类型用于存储字节数组。在SQL Server中,支持byte[]类型。

在您的情况下,您首先使用了Sqlite,然后更改为使用SQL Server。因此,您需要删除迁移脚本历史记录。

然后为SQL Server数据库创建并应用初始迁移:

dotnet ef migrations add init -o Data\Migrations
dotnet ef database update

然后表将在您的SQL Server数据库中创建。您的应用程序将准备就绪。

英文:

In Sqlite, type BLOB is used for storing byte array. In SQL Server, type byte[] is supported.

In your case, you used Sqlite at first, then you changed to use SQL Server. So, you need to remove the migration script history.

如何解决SQL在代码优先迁移部署Azure期间无法找到数据类型BLOB的问题

And then create and apply Initial Migration for SQl Server database:

dotnet ef migrations add init -o Data\Migrations
dotnet ef database update

And then tables will be created in your SQL Server database. And your application will be ready to run.

huangapple
  • 本文由 发表于 2020年1月3日 15:38:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定