В консольное приложение .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: значение не может быть нулевым. Имя параметра:
собственность на
Microsoft.EntityFrameworkCore.Utilities.Check.NotNull [T] (значение T,
String parameterName) в
Microsoft.EntityFrameworkCore.MutablePropertyBaseExtensions.SetPropertyAccessMode (IMutablePropertyBase
свойство, 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)
System.Lazy 1.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 [Т] (IServiceProvider
поставщик) в
Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies ()
в
Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider ()
в
Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService [TService] (IInfrastructure 1
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
name, String outputDir, String contextType) в
Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl (String
name, String outputDir, String contextType) в
Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase. <> C__DisplayClass3_0`1.b__0 ()
в
Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute (Action
action) Значение не может быть нулевым. Имя параметра: свойство
В чем проблема? Я потратил два часа на поиски в Google, и я не нашел ответа. Например, я посмотрел здесь: Значение Add-Migration не может быть нулевым. Название параметра: язык