我正在尝试将一个非常简单的代码优先示例从我的本地 SQL 移动到 Azure SQL,看起来我已经碰壁了。
这是我正在使用的简单代码:
public class Cat
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ID { get; set; }
public string Name { get; set; }
}
**Context class:**
public class CatDbContext : DbContext
{
public PersonDbContext()
: base("name=MyConnectionString")
{ }
public DbSet<Cat> Cats { get; set; }
}
My connection string:
<add name="MyConnectionString" connectionString="Server=tcp:xxxx.database.windows.net;Database=xxxx;User ID=xxxx@xxxx;Password=xxxx;Trusted_Connection=False;Encrypt=True;" providerName="System.Data.SqlClient" />
现在我有一个简单的控制台应用程序,它执行以下操作:
CatDbContext db = new CatDbContext();
db.Cats.Add(new Cat { Name = "Garfield", ID = Guid.NewGuid() });
db.SaveChanges();
我在 db.SaveChanges(); 行遇到了一个异常。说:无效的对象名称'dbo.Cats'。
堆栈跟踪:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
并且没有创建表。如果有人能指出我正确的方向,我将非常感激,因为我不知道为什么会这样。
亲切的问候,奥德里斯。