Reputation: 43
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?
Upvotes: 3
Views: 5078
Reputation: 5715
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).
Upvotes: 2