我是新的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学习当我写update-database时,我收到了这个错误:
第119行指定' -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 []参数, 文化信息文化) System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数,CultureInfo文化)
在System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr,Binder binder,Object [] index,CultureInfo system.Reflection.RuntimePropertyInfo.GetValue(Object。) obj,Object [] index)at System.Data.Entity.Core.Objects.ELinq.QueryParameterExpression.TryGetFieldOrPropertyValue(MemberExpression 我,对象实例,对象& memberValue)at System.Data.Entity.Core.Objects.ELinq.QueryParameterExpression.TryEvaluatePath(表达式 表达式,ConstantExpression& constantExpression) System.Data.Entity.Core.Objects.ELinq.QueryParameterExpression.EvaluateParameter(对象[] 争论 System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(可空1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery
1<> c__DisplayClass7.b__6() 在 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction [T](Func键1 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](Func键1 operation) at System.Data.Entity.Core.Objects.ObjectQuery
1.GetResults(可空1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery
1..GetEnumerator> b__0() 在System.Data.Entity.Internal.LazyEnumerator1.MoveNext() at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable
1来源) 在 System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.b__2 [TResult](IEnumerable的1 sequence) at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable
1 查询,表达式queryRoot)at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute [TResult](式 表达)at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute [TResult](式 表达)at System.Linq.Queryable.SingleOrDefault [TSource](IQueryable的1 source)
1.OnSeed(的DbContext System.Data.Entity.Migrations.DbMigrator.SeedDatabase()上下文) 在 System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.SeedDatabase() 在System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable
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)
1 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)at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(动作 mustSucceedToKeepDatabase)at System.Data.Entity.Migrations.DbMigrator.Update(字符串 targetMigration) System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(字符串 targetMigration) System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() 在System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() 非静态方法需要目标。
,
var eInDb = context.Enrollments.Where(s => s.Student.ID == e.Student.ID
&& s.Course.CourseID == e.CourseID).SingleOrDefault();
我的Configuration.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&GT; Update-Database -Verbose使用StartUp项目 &#39; ContosoUniversity&#39 ;.使用NuGet项目&#39; ContosoUniversity&#39;。指定 &#39; -Verbose&#39;用于查看正在应用于的SQL语句的标志 目标数据库。目标数据库是:&#39; ContosoUniversity2&#39; (数据源: (LocalDb)\ v11.0,Provider:System.Data.SqlClient,Origin: 组态)。没有待定的显式迁移。运行种子方法。 System.Reflection.TargetException:非静态方法需要一个 目标。在 System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
在System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数, 文化信息文化) System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数,CultureInfo文化)
在System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr,Binder binder,Object [] index,CultureInfo system.Reflection.RuntimePropertyInfo.GetValue(Object。) obj,Object [] index)at System.Data.Entity.Core.Objects.ELinq.QueryParameterExpression.TryGetFieldOrPropertyValue(MemberExpression 我,对象实例,对象&amp; memberValue)at System.Data.Entity.Core.Objects.ELinq.QueryParameterExpression.TryEvaluatePath(表达式 表达式,ConstantExpression&amp; constantExpression) System.Data.Entity.Core.Objects.ELinq.QueryParameterExpression.EvaluateParameter(对象[] 争论 System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(可空1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery
1&LT;&GT; c__DisplayClass7.b__6() 在 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction [T](Func键1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) at System.Data.Entity.Core.Objects.ObjectQuery
1&LT;&GT; c__DisplayClass7.b__5() 在 System.Data.Entity.Infrastructure.DbExecutionStrategy.Execute [TResult](Func键1 operation) at System.Data.Entity.Core.Objects.ObjectQuery
1.GetResults(可空1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery
1..GetEnumerator&GT; b__0() 在System.Data.Entity.Internal.LazyEnumerator1.MoveNext() at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable
1来源) 在 System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.b__2 [TResult](IEnumerable的1 sequence) at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable
1 查询,表达式queryRoot)at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute [TResult](式 表达)at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute [TResult](式 表达)at System.Linq.Queryable.SingleOrDefault [TSource](IQueryable的1 source)
1.OnSeed(的DbContext System.Data.Entity.Migrations.DbMigrator.SeedDatabase()上下文) 在 System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.SeedDatabase() 在System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable
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)
1 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&LT;&GT; c__DisplayClasse.b__d() 在 System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(动作 mustSucceedToKeepDatabase)at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(动作 mustSucceedToKeepDatabase)at System.Data.Entity.Migrations.DbMigrator.Update(字符串 targetMigration) System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(字符串 targetMigration) System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() 在System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() 非静态方法需要目标。
答案 0 :(得分:0)
正在收到此特定错误。原来是种子数据中有关用户创建的代码。
请密切注意ApplicationUser.UserName
-我用破折号表示它无法创建用户。