c# - 实体框架代码优先迁移失败,更新数据库
问题描述
我首先是新的 EF 代码。我从https://docs.microsoft.com/tr-tr/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity学习-framework-in-an-asp-net-mvc-application当我写更新数据库时,我得到这个错误:
指定“-Verbose”标志以查看应用于目标数据库的 SQL 语句。应用显式迁移:[201806161013068_ColumnFirstName]。应用显式迁移:201806161013068_ColumnFirstName。警告:更改对象名称的任何部分都可能破坏脚本和存储过程。运行种子方法。System.Reflection.TargetException:非静态方法需要一个目标。在 System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
在 System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] 参数, CultureInfo 文化) 在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr、Binder binder、Object[] 参数、CultureInfo 文化)
在 System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index) 在 System.Data.Entity。 Core.Objects.ELinq.QueryParameterExpression.TryGetFieldOrPropertyValue(MemberExpression me, Object instance, Object& memberValue) at System.Data.Entity.Core.Objects.ELinq.QueryParameterExpression.TryEvaluatePath(Expression expression, ConstantExpression& constantExpression) at System.Data.Entity.Core System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable) 处的 .Objects.ELinq.QueryParameterExpression.EvaluateParameter(Object[] arguments)1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery
1.<>c__DisplayClass7.b__6() 在 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) at System.Data.Entity.Core.Objects.ObjectQuery
1.<>c__DisplayClass7.b__5() 在 System.Data.Entity.Infrastructure.DbExecutionStrategy.Execute[ TResult](Func1 operation) at System.Data.Entity.Core.Objects.ObjectQuery
1.GetResults(Nullable1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery
1..GetEnumerator>b__0() at System.Data.Entity.Internal.LazyEnumerator1.MoveNext() at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable
1 source) 在 System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.b__2[TResult]( IEnumerable1 sequence) at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable
1 查询,表达式 queryRoot) 在 System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression 表达式) 在 System.Data.Entity.Internal.Linq.DbQueryProvider.Execute [TResult](表达式表达式)在 System.Linq.Queryable.SingleOrDefault[TSource](IQueryable1 source)
1.OnSeed(DbContext context) 在 System.Data.Entity.Migrations.DbMigrator.SeedDatabase() 在 System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.SeedDatabase() 在 System.Data.Entity.Migrations.DbMigrator.Upgrade( IEnumerable 1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at ContosoUniversity.Migrations.Configuration.Seed(SchoolContext context) in C:\Users\Bengisu\source\repos\ContosoUniversity\ContosoUniversity\Migrations\Configuration.cs:line 119 at System.Data.Entity.Migrations.DbMigrationsConfiguration1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable
在 System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) 在 System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClasse.b__d() 在 System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(操作 mustSucceedToKeepDatabase)在 System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) 在 System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) 在 System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration) 在 System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() 在 System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() 非静态方法需要一个目标。
在第 119 行,有
var eInDb = context.Enrollments.Where(s => s.Student.ID == e.Student.ID
&& s.Course.CourseID == e.CourseID).SingleOrDefault();
我的配置.cs:
internal sealed class Configuration : DbMigrationsConfiguration<ContosoUniversity.Data_Access_Layer.SchoolContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(SchoolContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data.
var students = new List<Student>
{
new Student { FirstMidName = "Carson", LastName = "Alexander",
EnrollmentDate = DateTime.Parse("2010-09-01") },
new Student { FirstMidName = "Meredith", LastName = "Alonso",
EnrollmentDate = DateTime.Parse("2012-09-01") },
new Student { FirstMidName = "Arturo", LastName = "Anand",
EnrollmentDate = DateTime.Parse("2013-09-01") },
new Student { FirstMidName = "Gytis", LastName = "Barzdukas",
EnrollmentDate = DateTime.Parse("2012-09-01") },
new Student { FirstMidName = "Yan", LastName = "Li",
EnrollmentDate = DateTime.Parse("2012-09-01") },
new Student { FirstMidName = "Peggy", LastName = "Justice",
EnrollmentDate = DateTime.Parse("2011-09-01") },
new Student { FirstMidName = "Laura", LastName = "Norman",
EnrollmentDate = DateTime.Parse("2013-09-01") },
new Student { FirstMidName = "Nino", LastName = "Olivetto",
EnrollmentDate = DateTime.Parse("2005-08-11") }
};
students.ForEach(s => context.Students.AddOrUpdate(p => p.LastName, s));
context.SaveChanges();
var courses = new List<Course>
{
new Course {CourseID = 1050, Title = "Chemistry", Credits = 3, },
new Course {CourseID = 4022, Title = "Microeconomics", Credits = 3, },
new Course {CourseID = 4041, Title = "Macroeconomics", Credits = 3, },
new Course {CourseID = 1045, Title = "Calculus", Credits = 4, },
new Course {CourseID = 3141, Title = "Trigonometry", Credits = 4, },
new Course {CourseID = 2021, Title = "Composition", Credits = 3, },
new Course {CourseID = 2042, Title = "Literature", Credits = 4, }
};
courses.ForEach(s => context.Courses.AddOrUpdate(p => p.Title, s));
context.SaveChanges();
var enrollments = new List<Enrollment>
{
new Enrollment {
StudentID = students.Single(s => s.LastName == "Alexander").ID,
CourseID = courses.Single(c => c.Title == "Chemistry" ).CourseID,
Grade = Grade.A
},
new Enrollment {
StudentID = students.Single(s => s.LastName == "Alexander").ID,
CourseID = courses.Single(c => c.Title == "Microeconomics" ).CourseID,
Grade = Grade.C
},
new Enrollment {
StudentID = students.Single(s => s.LastName == "Alexander").ID,
CourseID = courses.Single(c => c.Title == "Macroeconomics" ).CourseID,
Grade = Grade.B
},
new Enrollment {
StudentID = students.Single(s => s.LastName == "Alonso").ID,
CourseID = courses.Single(c => c.Title == "Calculus" ).CourseID,
Grade = Grade.B
},
new Enrollment {
StudentID = students.Single(s => s.LastName == "Alonso").ID,
CourseID = courses.Single(c => c.Title == "Trigonometry" ).CourseID,
Grade = Grade.B
},
new Enrollment {
StudentID = students.Single(s => s.LastName == "Alonso").ID,
CourseID = courses.Single(c => c.Title == "Composition" ).CourseID,
Grade = Grade.B
},
new Enrollment {
StudentID = students.Single(s => s.LastName == "Anand").ID,
CourseID = courses.Single(c => c.Title == "Chemistry" ).CourseID
},
new Enrollment {
StudentID = students.Single(s => s.LastName == "Anand").ID,
CourseID = courses.Single(c => c.Title == "Microeconomics").CourseID,
Grade = Grade.B
},
new Enrollment {
StudentID = students.Single(s => s.LastName == "Barzdukas").ID,
CourseID = courses.Single(c => c.Title == "Chemistry").CourseID,
Grade = Grade.B
},
new Enrollment {
StudentID = students.Single(s => s.LastName == "Li").ID,
CourseID = courses.Single(c => c.Title == "Composition").CourseID,
Grade = Grade.B
},
new Enrollment {
StudentID = students.Single(s => s.LastName == "Justice").ID,
CourseID = courses.Single(c => c.Title == "Literature").CourseID,
Grade = Grade.B
}
};
foreach(Enrollment e in enrollments)
{
var eInDb = context.Enrollments.Where(s => s.Student.ID == e.Student.ID
&& s.Course.CourseID == e.CourseID).SingleOrDefault();
//if the enrollment is not found in the database
if (eInDb == null)
{
context.Enrollments.Add(e);
}
}
context.SaveChanges();
}
}
}
使用 -verbose 选项:
PM> Update-Database –Verbose 使用启动项目“ContosoUniversity”。使用 NuGet 项目“ContosoUniversity”。指定“-Verbose”标志以查看应用于目标数据库的 SQL 语句。目标数据库是:“ContosoUniversity2”(数据源:(LocalDb)\v11.0,提供者:System.Data.SqlClient,来源:配置)。没有未决的显式迁移。运行种子方法。System.Reflection.TargetException:非静态方法需要一个目标。在 System.Reflection.RuntimeMethodInfo.CheckConsistency(对象目标)
在 System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] 参数, CultureInfo 文化) 在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] 参数, CultureInfo文化)
在 System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index) 在 System.Data.Entity。 Core.Objects.ELinq.QueryParameterExpression.TryGetFieldOrPropertyValue(MemberExpression me, Object instance, Object& memberValue) at System.Data.Entity.Core.Objects.ELinq.QueryParameterExpression.TryEvaluatePath(Expression expression, ConstantExpression& constantExpression) at System.Data.Entity.Core System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable) 处的 .Objects.ELinq.QueryParameterExpression.EvaluateParameter(Object[] arguments)1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery
1.<>c__DisplayClass7.b__6() 在 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) at System.Data.Entity.Core.Objects.ObjectQuery
1.<>c__DisplayClass7.b__5() 在 System.Data.Entity.Infrastructure.DbExecutionStrategy.Execute[ TResult](Func1 operation) at System.Data.Entity.Core.Objects.ObjectQuery
1.GetResults(Nullable1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery
1..GetEnumerator>b__0() at System.Data.Entity.Internal.LazyEnumerator1.MoveNext() at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable
1 source) 在 System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.b__2[TResult]( IEnumerable1 sequence) at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable
1 查询,表达式 queryRoot) 在 System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression 表达式) 在 System.Data.Entity.Internal.Linq.DbQueryProvider.Execute [TResult](表达式表达式)在 System.Linq.Queryable.SingleOrDefault[TSource](IQueryable1 source)
1.OnSeed(DbContext context) 在 System.Data.Entity.Migrations.DbMigrator.SeedDatabase() 在 System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.SeedDatabase() 在 System.Data.Entity.Migrations.DbMigrator.Upgrade( IEnumerable 1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at ContosoUniversity.Migrations.Configuration.Seed(SchoolContext context) in C:\Users\Bengisu\source\repos\ContosoUniversity\ContosoUniversity\Migrations\Configuration.cs:line 119 at System.Data.Entity.Migrations.DbMigrationsConfiguration1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable
在 System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) 在 System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClasse.b__d() 在 System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(操作 mustSucceedToKeepDatabase)在 System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) 在 System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) 在 System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration) 在 System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() 在 System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() 非静态方法需要一个目标。
解决方案
收到此特定错误。原来是种子数据中围绕用户创建的代码。
密切注意ApplicationUser.UserName
- 我放了破折号,它未能创建用户。
推荐阅读
- reactjs - 跨平台SDK
- oracle - Oracle:从程序内部更新不起作用
- r - 组合长和短刻面标签时裁剪条背景
- r - 无法通过 Rstudio 中的控制台安装 ggplot2
- python - Python 函数返回“列表列表”而不是平面列表
- python - 使用python3从兄弟姐妹中找不到模块
- c - 将 libIEC61850 库与 GTK3+ for C 链接时出现问题
- php - 如何在 CakePHP 4 的翻译值中添加配置参数
- azure - 错误 - 访问被拒绝 - 部署到 Azure 应用服务
- javascript - React Native map() 和数组中的 JSX 元素