【问题标题】:DataAccess Plugin in NopCommerce 3.1NopCommerce 3.1 中的数据访问插件
【发布时间】:2013-09-04 06:22:49
【问题描述】:

我正在尝试创建一个基于此tutorial 的具有数据访问权限的插件。

当调用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 forum上发布了这个问题。

【问题讨论】:

  • 当我添加这一行时,modelBuilder.Entity() .HasRequired(p => p.UsedWithOrder).WithOptional();我将遇到另一个错误.....数据库中已经有一个名为“Address_Country”的对象。无法创建约束或索引。查看以前的错误。

标签: c# entity-framework asp.net-mvc-4 nopcommerce


【解决方案1】:

在您的 OnModelCreating 方法中删除这一行

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

这一行是核心数据模型的参考

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-04
  • 2023-04-04
  • 1970-01-01
  • 2014-06-16
  • 1970-01-01
  • 2020-07-21
  • 1970-01-01
相关资源
最近更新 更多