Inhalte aufrufen

Profilbild

EF Migration In Plugin Error

Plugin EF Migration Error

2 Antworten zu diesem Thema

#1 nitware

nitware

    Member

  • Members
  • PunktPunkt
  • 11 Beiträge

Geschrieben: 12 October 2017 - 07:07

Hello team,

 

I'm writing a plugin that needs to persist data to database table. I successfully enable migration but having trouble adding migration. I get the below exception:

 

 

PM> add-migration GTPayInit
System.InvalidOperationException: Unable to determine the principal end of an association between the types 'SmartStore.Core.Domain.Customers.RewardPointsHistory' and 'SmartStore.Core.Domain.Orders.Order'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
   at System.Data.Entity.ModelConfiguration.Edm.Services.AssociationTypeMappingGenerator.GenerateIndependentAssociationType(AssociationType associationType, DbDatabaseMapping databaseMapping)
   at System.Data.Entity.ModelConfiguration.Edm.Services.AssociationTypeMappingGenerator.Generate(AssociationType associationType, DbDatabaseMapping databaseMapping)
   at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.GenerateAssociationTypes(DbDatabaseMapping databaseMapping)
   at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.Generate(EdmModel conceptualModel)
   at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
   at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
   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.ForceOSpaceLoadingForKnownEntityTypes()
   at System.Data.Entity.DbContext.System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext()
   at SmartStore.Data.ObjectContextBase..ctor(String nameOrConnectionString, String alias) in C:\Users\Dan\Documents\Repositories\estore\src\Libraries\SmartStore.Data\ObjectContextBase.cs:line 47
   at SmartStore.Data.ObjectContextBase..ctor() in C:\Users\Dan\Documents\Repositories\estore\src\Libraries\SmartStore.Data\ObjectContextBase.cs:line 32
   at SmartStore.GTPay.Data.GTPayObjectContext..ctor() in C:\Users\Dan\Documents\Repositories\estore\src\Plugins\SmartStore.GTPay\Data\GTPayObjectContext.cs:line 25
--- 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.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Scaffold(String migrationName, String language, String rootNamespace, Boolean ignoreChanges)
   at System.Data.Entity.Migrations.AddMigrationCommand.Execute(String name, Boolean force, Boolean ignoreChanges)
   at System.Data.Entity.Migrations.AddMigrationCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Unable to determine the principal end of an association between the types 'SmartStore.Core.Domain.Customers.RewardPointsHistory' and 'SmartStore.Core.Domain.Orders.Order'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
 
No modification was done to either SmartStore.Core.Domain.Orders.Order nor SmartStore.Core.Domain.Customers.RewardPointsHistory nor their mappings. I successfully added migration to SmartStore.Data. Why is that of the plugin failing? The code for the plugin ObjectContext is shown below:
 
 
 
public class GTPayObjectContext : ObjectContextBase
    {
        public const string ALIASKEY = "sm_object_context_gtpay";
 
        static GTPayObjectContext()
        {
            var initializer = new MigrateDatabaseInitializer<GTPayObjectContext, Configuration>
            {
                TablesToCheck = new[] { "GTPaySupportedCurrency", "GTPayTransactionLog", "GTPayTransactionStatus" }
            };
            Database.SetInitializer(initializer);
        }
 
        /// <summary>
        /// For tooling support, e.g. EF Migrations
        /// </summary>
        public GTPayObjectContext()
            : base()
        {
        }
 
        public GTPayObjectContext(string nameOrConnectionString)
            : base(nameOrConnectionString, ALIASKEY)
        {
        }
 
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new GTPayTransactionLogRecordMap());
            modelBuilder.Configurations.Add(new GTPaySupportedCurrencyRecordMap());
            modelBuilder.Configurations.Add(new GTPayTransactionStatusRecordMap());
 
            //disable EdmMetadata generation
            //modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
            base.OnModelCreating(modelBuilder);
        }
    }

 

Please assist, and thank you in advance.



#2 Marcus Gesing

Marcus Gesing

    SmartStore AG

  • Administrators
  • 3018 Beiträge

Geschrieben: 12 October 2017 - 11:12

"I successfully added migration to SmartStore.Data"
You must add it to your plugin project, not SmartStore.Data and don't forget to set the build configuration to EFMigrations. Please see the documentation for all details.
 
Example: The Google Merchant Center plugin. It uses fluent migration (see Data and Domain folders). It should look similar in your plugin.
 

Schöne Grüße aus Düsseldorf,
Marcus Gesing


#3 nitware

nitware

    Member

  • Members
  • PunktPunkt
  • 11 Beiträge

Geschrieben: 12 October 2017 - 13:50

Thank you for your assistance.





Auf dieses Thema antworten