我在.NET Core 2.1控制台应用程序中添加了以下代码:
public class ProductsContext : DbContext
{
public DbSet<Person> People { get; set; }
public DbSet<Product> Products { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PersonProduct>().HasKey(sc => new { sc.PersonId, sc.ProductId });
modelBuilder.Entity<Product>()
.ToTable("Product")
.HasDiscriminator<string>("Description")
.HasValue<Product1>("Product1")
.HasValue<Product2>("Product2");
modelBuilder.Entity<PersonProduct>()
.HasOne<Person>(sc => sc.Person)
.WithMany(s => s.PersonProduct)
.HasForeignKey(sc => sc.PersonId)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<PersonProduct>()
.HasOne<Product>(sc => sc.Product)
.WithMany(s => s.PersonProduct)
.HasForeignKey(sc => sc.ProductId)
.OnDelete(DeleteBehavior.Cascade);
var navigation = modelBuilder.Entity<Person>().Metadata.FindNavigation(nameof(Person));
navigation.SetPropertyAccessMode(PropertyAccessMode.Field);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=PQ;Trusted_Connection=True;MultipleActiveResultSets=true");
}
}
public abstract class Product
{
public Guid Id { get; private set; }
public string Description;
public List<PersonProduct> PersonProduct { get; set; }
public Product(Guid id)
{
Id = id;
}
}
public class Product1 : Product
{
public Product1(Guid id)
: base(id)
{
}
}
public class Product2 : Product
{
public Product2(Guid id)
: base(id)
{
}
}
public sealed class Person
{
public Guid Id { get; private set; }
public string Name { get; private set; }
//public List<PersonSport> PersonSport { get; private set; }
private readonly List<PersonProduct> _PersonProduct = new List<PersonProduct>();
public IEnumerable<PersonProduct> PersonProduct => _PersonProduct.AsReadOnly();
public Person(Guid id, string name)
{
Id = id;
Name = name;
}
public void AddEntry(PersonProduct PersonProduct)
{
_PersonProduct.Add(PersonProduct);
}
}
public sealed class PersonProduct
{
public Person Person { get; set; }
public Guid PersonId { get; set; }
public Product Product { get; set; }
public Guid ProductId { get; set; }
}
我已经在Nuget软件包管理器控制台中运行以下命令:ADD-MIGRATION InitialCreate
我得到的错误是:
System.ArgumentNullException:值不能为null。参数名称: 物业 Microsoft.EntityFrameworkCore.Utilities.Check.NotNull [T](T值, 字符串parameterName)在 Microsoft.EntityFrameworkCore.MutablePropertyBaseExtensions.SetPropertyAccessMode(IMutablePropertyBase 属性,可空
1 propertyAccessMode) at ConsoleApp1.ProductsContext.OnModelCreating(ModelBuilder modelBuilder) in C:\GenieDevelopment\REST\ConsoleApp1\Program.cs:line 42 at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator) at System.Lazy
1.ViaFactory(LazyThreadSafetyMode模式)
在System.Lazy1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) at System.Lazy
1.CreateValue()在 Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel() 在 Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite,ServiceProviderEngineScope范围) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite,ServiceProviderEngineScope范围) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite,ServiceProviderEngineScope范围) Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider 提供者,输入serviceType) Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService [T](IServiceProvider 提供者) Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
在 Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() 在 Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService [TService](IInfrastructure1 accessor) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func
1 工厂) Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType) Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String 名称,字符串outputDir,字符串contextType)位于 Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String 名称,字符串outputDir,字符串contextType)位于 Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase。<> c__DisplayClass3_0`1.b__0() 在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(操作 动作)值不能为null。参数名称:属性
出什么问题了?我花了两个小时在Google上搜索,但没有找到答案。例如,我看过这里:Add-Migration Value cannot be null. Parameter name: language
答案 0 :(得分:0)
似乎navigation
变量为空-FindNavigation
在类Person
中找不到名称为“ Person”的属性。
我想你想做这样的事情:
var navigation = modelBuilder.Entity<Person>().Metadata.FindNavigation(nameof(PersonProduct));
navigation.SetPropertyAccessMode(PropertyAccessMode.Field);