如何在实体框架中从另一部分类别生成的类中设置变量
我需要将一个构造函数添加到由EF自动生成的类中,从我的理解来增强EF创建的任何类,您将创建另一个具有相同名称的部分类别,因此例如我的EF生成的类
public partial class MyCoolClass // EF CLASS
{
public string VariableOne { get; set; }
public string VariableTwo { get; set; }
}
:创建另一个部分类别为:
public partial class MyCoolClass // MY CLASS
{
public MyCoolClass(Foo fooObject)
{
VariableOne = fooObject.Var1 // VariableOne doesn't exist
}
}
但是,我无法在EF生成的类中引用variable
variabletwo variabletwo ,您不会这样做吗?
我问这个问题的原因,是因为我的EF类大约具有20-30个变量,并且必须声明该类的新实例,然后在代码中填充每个字段很乏味,我想移动设置设置的逻辑变量成构造函数。
I need to add an constructor to a class which is automatically generated by EF, from my understand to enhance any class created by EF you'd create another partial class with the same name, so for example my EF generated class:
public partial class MyCoolClass // EF CLASS
{
public string VariableOne { get; set; }
public string VariableTwo { get; set; }
}
I'd then create another partial class as:
public partial class MyCoolClass // MY CLASS
{
public MyCoolClass(Foo fooObject)
{
VariableOne = fooObject.Var1 // VariableOne doesn't exist
}
}
However, I'm unable to reference VariableOne
and VariableTwo
in the EF generated class, can you not do this?
Reason I ask this question, is because my EF class has roughly 20-30 variables and having to declare a new instance of that class and then populate each field within the code is tedious, and I'd like to move that logic of setting the variables into a constructor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
丢失变量的唯一原因可能是您在不同的名称空间中都有两个类。部分类通常是将同一类分为两个或多个文件。
因此,给两个类别的名称空间相同,独立于将其放置在哪里,然后重试:
文件夹:实体
文件夹:模型(例如...)
...,所有属性都再次存在。
The only reason for the missing variable can be that you have both classes in different namespaces. Partial classes are generally the same class split into 2 or more files.
So, give the both classes the same namespace, independent from where are they placed and try again:
Folder: Entities
Folder: Models (for instance ...)
... and all Properties are there again.