3

I haven't seen question like this been raised. So, I created one for EF fluent api developers and nopcommerce plugin developers.

Here's what I'm trying to do:

I have a product entity and can used by EF to generate database. I want to extending the product entity without modify original class. So, I've been try to use partial classes. here's the code look like:

namespace Nop.Core.Domain.Catalog
{
    /// <summary>
    /// Represents a product
    /// </summary>
    public partial class Product : BaseEntity, ILocalizedEntity, ISlugSupported, IAclSupported, IStoreMappingSupported
    {
        //some fields here.
        public int ProductTypeId { get; set; }
        //.....
    }
}

Now when I extend the class like this:

namespace Nop.Core.Domain.Catalog
{
    public partial class Product : BaseEntity
    {
        public int RestaurantId { get; set; }

        public virtual Restaurant BelongRestaurant { get; set; } 
     }
}

It will throw an error.

The type 'Nop.Core.Domain.Catalog.Product' and the type 'Nop.Core.Domain.Catalog.Product' both have the same simple name of 'Product' and so cannot be used in the same model. All types in a given model must have unique simple names. Use 'NotMappedAttribute' or call Ignore in the Code First fluent API to explicitly exclude a property or type from the model.

Here is my mapping file look like:

namespace Nop.Plugin.Misc.Plugin
{
    public partial class ProductMap : EntityTypeConfiguration<Nop.Core.Domain.Catalog.Product>
    {
        public ProductMap()
        {
            //Table
            this.ToTable(Settings.DATABASE_TABLE_NAME);

            //Primary Key
            this.HasKey(t => t.Id);

            //Property
            this.Property(t => t.Id)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            this.HasRequired(p => p.BelongRestaurant)
                .WithMany(r => r.Menu)
                .HasForeignKey(p => p.RestaurantId)
                .WillCascadeOnDelete(false);
        }
    }
}

Could anyone help?

Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
Yuxuan Xue
  • 43
  • 1
  • 6

1 Answers1

2

partial class is just a syntax-sugar that allows you to have multiple files for one class. That is you can create as many partial classes in the same project.

But when you consume a class on another project, even if it's a partial class, you cannot create another file with partial class to extend its functionality.

Read more about Partial Classes and Methods (C# Programming Guide).

Ekk
  • 5,627
  • 19
  • 27
  • thanks so much, so, it's because they are not in same assembly, is there another way to work around it? – Yuxuan Xue Aug 23 '14 at 15:22
  • You can use Reflection to inject code to classes on another project, it's a bit too extreme though. – Ekk Aug 24 '14 at 02:18
  • Ekk, I like how your answer brings clarity as to why the original question couldn't be resolved with partial classes, but do you have any idea on how to do what Yuxuan wanted, i.e adding stuff to entities from another assembly? Or create extensible entities? – Michiel Cornille Feb 10 '16 at 08:10