为什么当我通过泛型传递实体时,属性是重复的,而不是所需的存储在数据库中?

我有一个nestedSetBuilder班。它具有MakeRootAsync方法:

public async Task<TEntity> MakeRootAsync<TEntity>(TEntity ownerNode) where TEntity: nestedSetEntity
{
    _operation = OperationmakeRoot;
    ownerNode.Lft = 1;
    ownerNode.Rgt = 2;
    ownerNode.Depth = 0;
    await _db.Set<TEntity>().AddAsync(ownerNode);
    await _db.SaveChangesAsync();
    return ownerNode;
}

有一个基类nestedSetEntity

public class nestedSetEntity
{
    public Guid Id { get; set; }
    public int Lft { get; set; }
    public int Rgt { get; set; }
    public int Depth { get; set; }
    public Guid? Tree { get; set; }
}

有一个子班Category

[Table("categories")]
public class Category: nestedSetEntity
{
    public Category()
    {
        Visible = true;
        CreatedAt = DateTime.Now;
        UpdatedAt = DateTime.Now;
    }

    [Column("id")]
    public Guid Id { get; set; }

    [Required]
    [StringLength(256)]
    [Column("title")]
    public string Title { get; set; }

    [Column("lft")]
    public int Lft { get; set; }

    [Column("rgt")]
    public int Rgt { get; set; }

    [Column("depth")]
    public int Depth { get; set; }

    [Column("tree")]
    public Guid? Tree { get; set; }

    [Column("visible")]
    public bool Visible { get; set; }

    [Required]
    [Column("created_at")]
    public DateTime CreatedAt { get; set; }

    [Column("updated_at")]
    public DateTime UpdatedAt { get; set; }
}

有一种方法叫makeRootAsync

[HttpGet]
public async Task<IactionResult> Categories()
{
    //var res = await _dnsParserService.ParseCategoriesAsync();
    var res = await _categoryParserService.ParseCategoryListAsync();

    var categoryIds = new Dictionary<string,string>();

    foreach (var categoryListResItem in res)
    {
        if (categoryIds.TryGetvalue(categoryListResItem.CategoryFirstTitle,out var parentCategory)) 
            continue;

        var node = Map(categoryListResItem,"CategoryFirstTitle");
        var addedCategory = await _nestedSetBuilder.MakeRootAsync(node); // this call
        categoryIds[categoryListResItem.CategoryFirstTitle] = addedCategory.Id.ToString();
    }

    return Ok(res);
}

在数据库中,列LftRgtDepth的值为零:

为什么当我通过泛型传递实体时,属性是重复的,而不是所需的存储在数据库中?

在调试器中,您可以看到字段是重复的(分别是基类和子类的字段):

为什么当我通过泛型传递实体时,属性是重复的,而不是所需的存储在数据库中?

告诉我如何解决?我使用基类来处理LINQ

zidaneii 回答:为什么当我通过泛型传递实体时,属性是重复的,而不是所需的存储在数据库中?

这可能是由于派生类hide the properties in your base class中的属性所致。根据您的需要,建议您在基类上设置Column属性,并删除派生的类属性:

public class NestedSetEntity
{
    [Column("id")]
    public Guid Id { get; set; }

    [Column("lft")]
    public int Lft { get; set; }

    [Column("rgt")]
    public int Rgt { get; set; }

    [Column("depth")]
    public int Depth { get; set; }

    [Column("tree")]
    public Guid? Tree { get; set; }
}

[Table("categories")]
public class Category: NestedSetEntity
{
    public Category()
    {
        Visible = true;
        CreatedAt = DateTime.Now;
        UpdatedAt = DateTime.Now;
    }

    [Required]
    [StringLength(256)]
    [Column("title")]
    public string Title { get; set; }

    [Column("visible")]
    public bool Visible { get; set; }

    [Required]
    [Column("created_at")]
    public DateTime CreatedAt { get; set; }

    [Column("updated_at")]
    public DateTime UpdatedAt { get; set; }
}

如果从NestedSetEntity继承的不同表需要不同的列名,则可以使用接口而不是基类:

public interface NestedSetEntity
{
    Guid Id { get; set; }
    int Lft { get; set; }
}

[Table("categories")]
public class Category : NestedSetEntity
{
    [Required]
    [Column("title")]
    public string Title { get; set; }

    [Column("id")]
    public Guid Id { get; set; }

    [Column("lft")]
    public int Lft { get; set; }
}

[Table("mytable")]
public class MyTable : NestedSetEntity
{
    [Column("my_id")]
    public Guid Id { get; set; }

    [Column("left_column")]
    public int Lft { get; set; }
}
本文链接:https://www.f2er.com/3133121.html

大家都在问