Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么,当我通过泛型传递实体时,属性是重复的,而数据库中不存储所需的内容?_C#_.net_Entity Framework_Asp.net Core_.net Core - Fatal编程技术网

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

C# 为什么,当我通过泛型传递实体时,属性是重复的,而数据库中不存储所需的内容?,c#,.net,entity-framework,asp.net-core,.net-core,C#,.net,Entity Framework,Asp.net Core,.net Core,我有一个NestedSetBuilder类。它有一个MakeRootAsync方法: public async Task<TEntity> MakeRootAsync<TEntity>(TEntity ownerNode) where TEntity: NestedSetEntity { _operation = OperationMakeRoot; ownerNode.Lft = 1; ownerNode.Rgt = 2; ownerNo

我有一个
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;
}
有一个子类
类别

[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);
}
[HttpGet]
公共异步任务类别()
{
//var res=await_dnsParserService.ParseCategoriesAsync();
var res=await_categoryParserService.parseCategoryListSync();
var categoryId=新字典();
foreach(以res为单位的var Categorylisteritem)
{
if(categoryIds.TryGetValue(categoryListem.CategoryFirstTitle,out var parentCategory))
继续;
变量节点=地图(CategoryListItem,“CategoryFirstTitle”);
var addedCategory=await _nestedSetBuilder.MakeRootAsync(node);//此调用
CategoryId[CategoryListem.CategoryFirstTitle]=addedCategory.Id.ToString();
}
返回Ok(res);
}
在数据库中,
Lft
Rgt
Depth
列的值为零:

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


告诉我怎么修?我使用基类来处理LINQ,这可能是由于派生类中的属性。根据需要,我建议在基类上设置
属性,并删除派生类属性:

公共类NestedSetEntity
{
[列(“id”)]
公共Guid Id{get;set;}
[列(“lft”)]
公共整数Lft{get;set;}
[列(“rgt”)]
公共int Rgt{get;set;}
[列(“深度”)]
公共整数深度{get;set;}
[列(“树”)]
公共Guid?树{get;set;}
}
[表(“类别”)]
公共类类别:NestedSetEntity
{
公共类别()
{
可见=真实;
CreatedAt=DateTime.Now;
UpdatedAt=DateTime.Now;
}
[必需]
[StringLength(256)]
[栏目(“标题”)]
公共字符串标题{get;set;}
[列(“可见”)]
公共布尔可见{get;set;}
[必需]
[列(“创建于”)]
public DateTime CreatedAt{get;set;}
[栏目(“更新地址”)]
public DateTime UpdatedAt{get;set;}
}
如果继承自
NestedSetEntity
的不同表需要不同的列名,则可以使用接口而不是基类:

公共接口NestedSetEntity
{
Guid Id{get;set;}
int Lft{get;set;}
}
[表(“类别”)]
公共类类别:NestedSetEntity
{
[必需]
[栏目(“标题”)]
公共字符串标题{get;set;}
[列(“id”)]
公共Guid Id{get;set;}
[列(“lft”)]
公共整数Lft{get;set;}
}
[表格(“mytable”)]
公共类MyTable:NestedSetEntity
{
[栏(“我的id”)]
公共Guid Id{get;set;}
[列(“左_列”)]
公共整数Lft{get;set;}
}

如果接口中只能声明方法,如何使用EntityClass接口?接口可以包含方法、属性、事件、索引器或这四种成员类型的任意组合。请参阅,我已更新了答案,以包含
NestedSetEntity
是接口的代码。
[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);
}