C#自动生成的部分类重新定义属性获取设置 [英] c# auto generated partial class redefine property get set

查看:120
本文介绍了C#自动生成的部分类重新定义属性获取设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正从主要的经典ASP迁移到.NET。因此,这可能是一个愚蠢的问题,但我找不到答案。



我有一个使用Database First和Entity Framework的MVC应用程序。现在,我想为自动生成的 partial类添加一些逻辑。根据我的阅读,应该创建一个具有相同名称空间和名称的新部分类。但是,当我这样做时,我得到一个错误(此成员被多次定义)和 [部分类]和[部分类]之间的歧义。我理解错误在说什么,但是我不确定如何解决该问题。



我想在集合中添加一些逻辑;



因此在生成的类中,我有

 公共部分class QualityChecks 
{
.....
public int DailyCount {get;组; }
...
}

在我的新部分课程中,我想添加到设置代码,以确保仅添加大于0的值。如果添加了负值,则需要记录该值并将其更改为0
例如我新的局部类是:

 公共局部类QualityChecks {
public int DailyCount {
set
{
DailyCount =值;
,如果< 0 log并设置为0
}

}

如果尚不清楚,这可能会有所帮助:
当前,我有很多简单的代码

  QualityChecks qc =新的QualityChecks (); 
qc.DailyCount =输入的金额;
....
db.QualityChecks.add(qc);

然后在任何地方更新该逻辑,最好将其包装在QualityChecks类中。 / p>

这是正确的做法吗?如果是这样,我需要做些什么才能使它起作用?



在此先感谢您提供任何提示和帮助!

解决方案

您不能在两个不同的文件中定义相同的成员。



您可以尝试定义新的包装器属性(例如。MyDailyCount),并添加了额外的逻辑并在最后更新基础的DailyCount,以便将其持久化到数据库中。

  public int MyDailyCount 
{
get {返回DailyCount; }
设置
{
DailyCount =值;
//您的额外逻辑
}
}


I'm moving from mainly classic asp to .NET. So this may be a stupid question, but I can't find an answer.

I have an MVC App using Database First and Entity Framework. Now I would like to add some logic to the auto generated 'partial' classes. From what I have read it should be a matter of creating a new partial class with the same namespace and name. But when I do that I get an error "(This member is defined more than once)" and "Ambiguity between [partial class] and [partial class]". I understand what the error is saying, but I'm not sure how to resolve the problem.

I would like to add some logic to the set; accessor.

So in the generated class I have

public partial class QualityChecks
{
.....
    public int DailyCount { get; set; }
...
}

in my new partial class I would like to add to the set code to make sure only values greater then 0 are added. If a negative value is added it needs to be logged and changed to 0 e.g. my new partial class is:

public partial class QualityChecks    {
public int DailyCount         {        
set
        {
            DailyCount = value;
            if it's < 0 log and set to 0
        }

    }

If that's not clear maybe this will help: Currently I have loads of code that simply does

QualityChecks qc = new QualityChecks();
qc.DailyCount = enteredAmount;
....
db.QualityChecks.add(qc);

Rather then update that logic everywhere it would be nice to have it wrapped up in the QualityChecks class.

Is this the right way of going about it? If so what do I need to change to make this work?

Thank you in advance for any tips and help!

解决方案

You cannot define the same members in two different files.

You can try to define a new wrapper property (eg. MyDailyCount) that add that extra logic and update the underlying DailyCount at the end so it get persisted to database.

public int MyDailyCount 
{
   get { return DailyCount; }
   set 
   { 
         DailyCount = value; 
         // your extra logic 
    }
}

这篇关于C#自动生成的部分类重新定义属性获取设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆