我正在尝试采用吉米·博加德的Contoso大学核心项目 https://github.com/jbogard/ContosoUniversityCore
我想先进行代码迁移,但不确定如何正确设置它。
我将 Migrator.EF6.Tools 添加到我的项目中。
当我运行启用迁移时
我收到此错误:
Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not
marked as serializable."
At C:\Users\SomeUser\.nuget\packages\entityframework\6.1.3\tools\EntityFramework.psm1:718 char:5
+ $domain.SetData('project', $project)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SerializationException
Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not
marked as serializable."
At C:\Users\SomeUser\.nuget\packages\entityframework\6.1.3\tools\EntityFramework.psm1:719 char:5
+ $domain.SetData('contextProject', $contextProject)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SerializationException
Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not
marked as serializable."
At C:\Users\SomeUser\.nuget\packages\entityframework\6.1.3\tools\EntityFramework.psm1:720 char:5
+ $domain.SetData('startUpProject', $startUpProject)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SerializationException
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Data.Entity.Migrations.Extensions.ProjectExtensions.GetPropertyValue[T](Project project, String propertyName)
at System.Data.Entity.Migrations.MigrationsDomainCommand.GetFacade(String configurationTypeName, Boolean useContextWorkingDirectory)
at System.Data.Entity.Migrations.EnableMigrationsCommand.FindContextToEnable(String contextTypeName)
at System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Object reference not set to an instance of an object.
这里真正的问题是 EF 有不同的“口味”。只需转到 EF 根文档即可查看差异:
- EF(根文档文件夹):https://learn.microsoft.com/en-us/ef/ https://learn.microsoft.com/en-us/ef/
- EF 6 迁移命令:https://dzone.com/articles/ef-migrations-command https://dzone.com/articles/ef-migrations-command
-
EF Core迁移命令:https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations
- .Net Core 中的迁移命令发生了很大变化,我建议
另请访问:https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet
以下是我使用 EF 6 和 .NET Core 进行迁移的秘诀:
1st.- 您必须将这些行添加到.csproj:
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.1" />
</ItemGroup>
注:版本已更改
2.- 创建上下文在你的项目中是这样的:
public class YourContext : DbContext
{
#region Constructors
public YourContext()
{
}
public YourContext(DbContextOptions options) : base(options)
{
}
#region DbSets // YOUR DB SETS GO HERE...
#endregion DbSets
#region OnConfiguring // THIS HELPED ME A LOT:
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
// In order to be able to create migrations and update database:
if (!options.IsConfigured)
{
options.UseSqlServer("YourLocalConnectionStringShouldBeHere");
}
base.OnConfiguring(options);
}
#endregion
#region Model Creating
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Your Model Crerating Stuff
}
#endregion Model Creating
}
3rd.- 添加迁移:
转到项目文件夹,from cmd, type:
dotnet ef migrations add [NameOFYourMigrationGoesHere] -c YourContext
注意:不要忘记将创建的文件添加到源代码管理,这不是自动完成的
4rd.- 更新您的数据库:
4.a.-在码头工人-> 您可以运行该项目(完全使用 Docker),迁移将在第一次使用 Context 时应用。
注意:(它将使用为该环境配置的连接字符串)
4.b.- 您的本地数据库-> 编辑在 ConfigurationContext.OnConfigure 中硬编码的连接字符串并运行(从 cmd 控制台):
dotnet ef database update --context ConfigurationContext
我希望它对你有帮助。
Juan
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)