简体   繁体   English

EF 6 Core 前缀基于多对一关系中实体的属性

[英]EF 6 Core prefix based on property of the entity in many-to-one relationship

I have two related entities:我有两个相关的实体:

[Index(nameof(Name), IsUnique = true)]
public class ApiResource : Entity<int>
{
    public string Name { get; set; }
    public ICollection<ApiScope> ApiScopes { get; set; }
}

and related entity:及相关实体:

[Index(nameof(Name), IsUnique = true)]
public class ApiScope : Entity<int>
{
    public string Name { get; set; }
    public ICollection<Client> Clients { get; set; }
    public ApiResource ApiResource { get; set; }
}

and I want to add prefix for ApiScope.Name based on ApiResource.Name.我想根据 ApiResource.Name 为 ApiScope.Name 添加前缀。 For example, if ApiResource.Name = "niceAPI", then for example ApiScope.Name = "niceAPI:test.read".例如,如果 ApiResource.Name = "niceAPI",那么例如 ApiScope.Name = "niceAPI:test.read"。 Is this possible to do without manually adding prefix when creating ApiResource and ApiScope?在创建 ApiResource 和 ApiScope 时是否可以不手动添加前缀? If yes, how?如果是,如何?

I think you can modify your setter of ApiScope.Name .我认为您可以修改ApiScope.Name的设置器。 Something like就像是

private string _name;
public string Name
{
    get => _name;
    set
    {
        string prefix = ApiResource.Name + ":";
        if (value.StartsWith(prefix))
            _name = value;
        else
            _name = prefix + value;
    }
}

But you should ensure ApiResource is set in advance.但是您应该确保提前设置了ApiResource

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM