简体   繁体   中英

Add-Migration exception: System.ArgumentNullException: Value cannot be null. Parameter name: property

I have the following code into a .NET Core 2.1 console application:

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; }
    }

I have run the following command in Nuget Package Manager Console: ADD-MIGRATION InitialCreate

The error I get is:

System.ArgumentNullException: Value cannot be null. Parameter name: property at Microsoft.EntityFrameworkCore.Utilities.Check.NotNull[T](T value, String parameterName) at Microsoft.EntityFrameworkCore.MutablePropertyBaseExtensions.SetPropertyAccessMode(IMutablePropertyBase property, Nullable 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 mode)
at System.Lazy 1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) at System.Lazy 1.CreateValue() at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel() at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() at Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService[TService](IInfrastructure 1 accessor) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func 1 factory) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType) at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) Value cannot be null. Parameter name: property

What is the problem? I have spent two hours Googling this and I have not found an answer. For example, I have looked here: Add-Migration Value cannot be null. Parameter name: language

Seems like the navigation variable is null - FindNavigation cannot find property of name "Person" in class Person .

I suppose you wanted to do something like this:

        var navigation = modelBuilder.Entity<Person>().Metadata.FindNavigation(nameof(PersonProduct));
        navigation.SetPropertyAccessMode(PropertyAccessMode.Field);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM