我是新来的。我有关于EF代码优先迁移的问题。我有数据库,但我想创建新的数据库。我添加了新的实体(Ax_Account和Ax_TaxGroup等),但是当我尝试运行“ Add-Migration NewDatabase”时,它抛出“序列不包含任何匹配元素”。我该如何解决?
例外:
PM> Add-Migration NewColumnsForProduct
System.InvalidOperationException: Sequence contains no matching element
at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
at System.Data.Entity.Core.Metadata.Edm.OSpaceTypeFactory.CreateAndAddNavigationProperty(StructuralType cspaceType, StructuralType ospaceType, NavigationProperty cspaceProperty)
at System.Data.Entity.Core.Metadata.Edm.OSpaceTypeFactory.<>c__DisplayClass1a.<TryFindNavigationProperties>b__16()
at System.Data.Entity.Core.Metadata.Edm.CodeFirstOSpaceLoader.LoadTypes(EdmItemCollection edmItemCollection, ObjectItemCollection objectItemCollection)
at System.Data.Entity.ModelConfiguration.Edm.DbDatabaseMappingExtensions.ToMetadataWorkspace(DbDatabaseMapping databaseMapping)
at System.Data.Entity.Internal.CodeFirstCachedMetadataWorkspace.Create(DbDatabaseMapping databaseMapping)
at System.Data.Entity.Infrastructure.DbModel.Compile()
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.ForceOSpaceLoadingForKnownEntityTypes()
at System.Data.Entity.DbContext.System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext()
at Master_Database_Project.Contexts.MasterDbContext..ctor() in C:\Users\Emre.dereli\source\repos\ProductCatalog\Master Database Project\Contexts\MasterDbContext.cs:line 52
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Data.Entity.Infrastructure.DbContextInfo.CreateInstance()
at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo, Func`1 resolver)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.RunCore()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
Sequence contains no matching element
Ax_Account.cs
[Table("Ax_Account")]
public class Ax_Account
{
public Guid Id { get; set; }
public string Number { get; set; }
public string Name { get; set; }
public string NameEnglish { get; set; }
public virtual ICollection<Product> Products { get; set; }
public static void OnModelCreating (DbModelBuilder modelBuilder) {
var e = modelBuilder.Entity<Ax_Account>();
e.HasKey(x => x.Id);
e.Property(x => x.Id).HasColumnName("id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
e.Property(x => x.Number).HasColumnName("number").HasMaxLength(50);
e.Property(x => x.Name).HasColumnName("name").HasMaxLength(250);
e.Property(x => x.NameEnglish).HasColumnName("name_english").HasMaxLength(250);
}
}
DbContext.cs
public class MasterDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Country> Countries { get; set; }
public DbSet<Language> Languages { get; set; }
public DbSet<ProductDetailsByCountry> ProdDetailsByCountry { get; set; }
public DbSet<ProductDetailsByLanguage> ProdDetailsByLanguage { get; set; }
public DbSet<Dimension> ProductCategories { get; set; }
public DbSet<DimensionType> DimensionTypes { get; set; }
public DbSet<Ax_Account> Ax_Accounts { get; set; }
public DbSet<Ax_TaxGroup> Ax_TaxGroups { get; set; }
public DbSet<DefectedProcessOrderLineServiceType> DefectedProcessOrderLineServiceTypes { get; set; }
public DbSet<EquipmentType> EquipmentTypes { get; set; }
public DbSet<InsuranceTerm> InsuranceTerms { get; set; }
public DbSet<InvoiceAlgorithmType> InvoiceAlgorithmTypes { get; set; }
public DbSet<InvoiceProductGroup> InvoiceProductGroups { get; set; }
public DbSet<ProductType> ProductTypes { get; set; }
public DbSet<PurchasingGroup> PurchasingGroups { get; set; }
public DbSet<Specification> Specifications { get; set; }
public DbSet<SpecificationDetailsByCountry> SpecificationDetailsByCountries { get; set; }
public DbSet<SpecificationDetailsByLanguage> SpecificationDetailsByLanguages { get; set; }
public DbSet<TimeAdjustmentType> TimeAdjustmentTypes { get; set; }
public DbSet<Unit> Units { get; set; }
public DbSet<VehicleType> VehicleTypes { get; set; }
public DbSet<WebBufferOfferLine> WebBufferOfferLines { get; set; }
static MasterDbContext ()
{
Database.SetInitializer(new MasterContextInitializer());
}
public MasterDbContext() : base("ProductCatalogDb")
{
var objContext = ((IObjectContextAdapter)this).ObjectContext;
// Update Last Changed fields for Product, DetailsByCountry, DetailsByLanguage when row is Added or Modified
objContext.SavingChanges += (sender, args) => {
var now = DateTimeOffset.Now;
foreach (var entry in this.ChangeTracker.Entries<Product>()) {
if (entry.State == EntityState.Modified || entry.State == EntityState.Added) {
entry.Entity.LastChangedDatetime = now;
}
}
foreach (var entry in this.ChangeTracker.Entries<ProductDetailsByCountry>()) {
if (entry.State == EntityState.Modified || entry.State == EntityState.Added) {
entry.Entity.LastChangedDatetime = now;
}
}
foreach (var entry in this.ChangeTracker.Entries<ProductDetailsByLanguage>()) {
if (entry.State == EntityState.Modified || entry.State == EntityState.Added) {
entry.Entity.LastChangedDatetime = now;
}
}
this.ChangeTracker.DetectChanges();
};
}
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
Product.OnModelCreating(modelBuilder);
Country.OnModelCreating(modelBuilder);
Language.OnModelCreating(modelBuilder);
User.OnModelCreating(modelBuilder);
ProductDetailsByCountry.OnModelCreating(modelBuilder);
ProductDetailsByLanguage.OnModelCreating(modelBuilder);
Dimension.OnModelCreating(modelBuilder);
DimensionType.OnModelCreating(modelBuilder);
Ax_Account.OnModelCreating(modelBuilder);
Ax_TaxGroup.OnModelCreating(modelBuilder);
DefectedProcessOrderLineServiceType.OnModelCreating(modelBuilder);
EquipmentType.OnModelCreating(modelBuilder);
InsuranceTerm.OnModelCreating(modelBuilder);
InvoiceAlgorithmType.OnModelCreating(modelBuilder);
InvoiceProductGroup.OnModelCreating(modelBuilder);
ProductType.OnModelCreating(modelBuilder);
PurchasingGroup.OnModelCreating(modelBuilder);
Specification.OnModelCreating(modelBuilder);
SpecificationDetailsByCountry.OnModelCreating(modelBuilder);
SpecificationDetailsByLanguage.OnModelCreating(modelBuilder);
TimeAdjustmentType.OnModelCreating(modelBuilder);
Unit.OnModelCreating(modelBuilder);
VehicleType.OnModelCreating(modelBuilder);
WebBufferOfferLine.OnModelCreating(modelBuilder);
}
}