Question

First of all and as you can see in the title, I'm designing a plugin for nopCommerce. I believe that the problem is not related to it (but with Entity Framework) but I'm not really sure.

My plugin has access to data, and nopCommerce let's me create my own context, creating the required tables and relations.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new ParentMap());
    modelBuilder.Configurations.Add(new ChildMap());
}

Conceptually, what I would like to have is:

ROOT TABLE ----(1:N)---- CHILDTABLE ----(1:1)---- EXISTING NOPCOMMERCE TABLE

And the tables and columns created in the DB seems correct to me. The mappings are pretty simple:

public ParentMap()
{
    ToTable("TableA");

    HasKey(m => m.Id);

    Property(m => m.SomeProperty);

    HasMany(m => m.Child).WithRequired(s => s.Parent).WillCascadeOnDelete();
}

public ChildMap()
{
    ToTable("TableB");

    HasKey(m => new {  m.Id });

    HasRequired(m => m.NopCommerceEntity);

    HasRequired(m => m.Parent);
}

Till this point, everything seems to work pretty well, things get registered and the application starts. When I try to insert or update anything that has relation with the nopCommerce entity, I get the following exception:

"The member with identity 'Namespace.ChildObject_NopCommerceEntity' does not exist in the metadata collection.\r\nParameter name: identity".

I've been thinking that the problem could be related with the fact that I'm not adding other mappings in my OnModelCreating. I've tried to add all the related mappings there but I get the same exception.

It's a bit complicated. I'd be very greatful for your help & time :)

Was it helpful?

Solution

You are right this issue related to Entity Framework because EF does not support cross-project POCO relations. You have to remove any property that has an entity from NopEntities in your plugin and instead use an Id of that entity as int as below :

public int ProductId { get; set; }

instead of

public virtual Product Product { get; set; }

and in the mapping you should not specify the relationship between your entity and the Nop.

OTHER TIPS

I think you can still use the following relation in your child class

     public int ProductId           { get; set; }
     public virtual Product Product { get; set; } 

but when you save or update the child class, instead of setting

child.Product = product

instead set the following

child.ProductId = product.Id

and also i think in your PluginObjectContext plugin class inherits from NopObjectContext.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top