NopCommerce 3.1中的DataAccess插件

本文关键字:DataAccess 插件 中的 NopCommerce | 更新日期: 2023-09-27 18:30:03

我正试图基于本教程创建一个具有数据访问功能的插件。

当调用Install方法时,它又调用CreateDatabaseInstallationScript。教程中插件生成的脚本仅为自定义表生成SQL

但当函数在我的插件中被调用时,它会为大约30个表生成SQL

以下是DbContext类:

public class MyProductObjectContext : DbContext, IDbContext
{
public MyProductObjectContext(string connectionString)
    : base(connectionString){}
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<RewardPointsHistory>()
                  .HasRequired(p => p.UsedWithOrder).WithOptional();
        modelBuilder.Configurations.Add(new MyProductMap());
        modelBuilder.Configurations.Add(new MyProductNoteMap());
        base.OnModelCreating(modelBuilder);
    }
    public string CreateDatabaseInstallationScript()
    {
        return ((IObjectContextAdapter)this).ObjectContext
                                   .CreateDatabaseScript();
    }
    public void Install()
    {
        //It's required to set initializer to null (for SQL Server Compact).
        //otherwise, you'll get something like "The model backing the 'your 
        //context name' context has changed since the database was created. 
        //Consider using Code First Migrations to update the database"
        Database.SetInitializer<MyProductObjectContext>(null);
        var scripts = CreateDatabaseInstallationScript();
        Database.ExecuteSqlCommand(scripts);
        SaveChanges();
    }
    public void Uninstall()
    {
        var dbScript = 
@"DROP TABLE LTMyProduct
GO
DROP TABLE LTMyProductNote
GO
";
        Database.ExecuteSqlCommand(dbScript);
        SaveChanges();
    }
    public new IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity
    {
        return base.Set<TEntity>();
    }
}

我的任何实体均未提及任何Nop.Core.Domain实体。实体代码如下:

public class MyProduct : BaseEntity
{
    public int CustomerId { get; set; }
    public Customer Customer { get; set; }
    public int ProductId { get; set; }
    //public virtual Product Product { get; set; }
    public string ProductName { get; set; }
    public int NotesCount { get; set; }
    public virtual ICollection<MyProductNote> Items { get; set; }
    public MyProduct()
    {
        Items = new HashSet<MyProductNote>();
    }
}

public class MyProductNote : BaseEntity
{
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string Note { get; set; }
    public int MyProductId { get; set; }
    public virtual MyProduct MyProduct { get; set; }
    public string FullName
    {
        get
        {
            return FirstName + 
                         (string.IsNullOrWhiteSpace(MiddleName) ? "" : 
                   (" " + MiddleName)) + " " + LastName;
        }
    }
}    

两个类的映射(EntityTypeConfiguration)如下:

public class MyProductMap : EntityTypeConfiguration<MyProduct>
{
    public MyProductMap()
    {
        ToTable("LTMyProduct");
        HasKey(x => x.Id);
        Property(x => x.CustomerId).IsRequired();
        Property(x => x.ProductId).IsRequired();
        Property(x => x.ProductName).IsUnicode()
            .IsVariableLength().HasMaxLength(250).IsOptional();
        HasMany(x => x.Items);
    }
}

public class MyProductNoteMap : EntityTypeConfiguration<MyProductNote>
{
    public MyProductNoteMap()
    {
        ToTable("LTMyProductNote");
        HasKey(x => x.Id);
        Property(x => x.FirstName).HasMaxLength(50).IsRequired()
                  .IsUnicode().IsVariableLength();
        Property(x => x.MiddleName).HasMaxLength(50).IsUnicode()
                  .IsVariableLength();
        Property(x => x.LastName).HasMaxLength(50).IsRequired().IsUnicode()
                   .IsVariableLength();
        Property(x => x.Note).IsVariableLength().HasMaxLength(1024)
                   .IsUnicode();
        Property(x => x.MyProductId).IsRequired().HasColumnType("int");
    }
}

我也在NopCommerce论坛上发布了这个问题。

NopCommerce 3.1中的DataAccess插件

删除OnModelCreating方法上的此行

modelBuilder.Entity().HasRequired(p=>p.UsedWithOrder).WithOptional();

这一行正在重新定义核心数据模型