Extend Entity framework 5 object using partial class and map it to existing other objects

I have not seen such an issue as raised. So I created paid api developers and nopcommerce plugin developers for EF developers.

Here's what I'm trying to do:

I have a product object and can be used by EF to create a database. I want to extend the product object without changing the original class. So, I was trying to use partial classes. here the code looks like this:

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 throws an error.

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

Here is my mapping file looks like this:

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);
        }
    }
}

      

Can anyone please help?

+3


source to share


1 answer


partial class

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

But when you use a class in another project, even if it is partial class

, you cannot create another file with partial class

to extend its functionality.



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

+2


source







All Articles