简体   繁体   English

实体框架多个属性的默认值

[英]Entity Framework default value for multiple properties

Is it possible to default mutiple value by only one related attribute? 是否可以仅通过一个相关属性来默认多个值? I find setting it row by row is a bit boilerish. 我发现逐行设置它有点笨拙。

public class FooBarCounter{
        [Key]
        public int id{ get; set; }
        [DefaultValue(0)]
        public int FooCounter1 { get; private set; }
        [DefaultValue(0)]
        public int FooCounter2 { get; private set; }

        [DefaultValue(5)]
        public int BarCounter1 { get; private set; }
        [DefaultValue(5)]
        public int BarCounter2 { get; private set; }        
    }

Edit: 编辑:

So the [Attrib] is in every second row, i would rather like something like this: 因此[Attrib]位于第二行,我希望这样:

[Authorize]
public class MyFullyAuthorizedClass : Controller {
   //props, methoods etcetc
}

Here the [Authorize] affects the entire class, like if i would put it before every methood, now thats not the best either, since i have multiple defaults, but it's only 1 line, not a boiler in every secont line. 这里的[Authorize]影响整个类,就像我把它放在每个聚会之前一样,现在那也不是最好的,因为我有多个默认值,但是它只有1行,而不是每个secont行中的锅炉。 Hope i'm clearer now. 希望我现在更清楚。

Any ideas? 有任何想法吗?

The DefaultValue attribute is valid on all targets - so you can set it at the class level if you like, and write code that looks for it and reacts to it. DefaultValue属性在所有目标上均有效 -因此您可以根据需要在类级别上对其进行设置,并编写代码对其进行查找并对其作出反应。

It's unlikely however that any Entity Framework implementations look for it and apply it - it's not part of the EF spec . 但是,任何Entity Framework实现都不太可能查找并应用它- 它不是EF规范的一部分

public class FooBarCounter{
    private const int DEFAULTVALUE = 0;

    [DefaultValue(DEFAULTVALUE)]
    public int FooCounter1 { get; private set; }
    [DefaultValue(DEFAULTVALUE)]
    public int FooCounter2 { get; private set; }
    // and so on ...

}

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

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