簡體   English   中英

NopCommerce 3.1中的DataAccess插件

[英]DataAccess Plugin in NopCommerce 3.1

我正在嘗試根據本教程創建具有數據訪問權限的插件。

調用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論壇 上發布了問題

在OnModelCreating方法上刪除此行

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

該行反映了核心數據模型

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM