[英]Is it possible to generate database fields without using C# properties in Entity Framework, code-first approach?
I am new on using the .NET Entity Framework (v6.1) , following the code-first approach. 按照代码优先方法,我刚开始使用.NET实体框架(v6.1) 。 I am using it on the creation of a native C# application.
我在创建本机C#应用程序时使用它。
I have created two classes, one base and one inherited: 我创建了两个类,一个是基类,一个是继承的类:
public BaseClass
{
public int ClassID { get; set; }
//Rest of class properties/ctors/function definitions.
}
public DerivedClass : BaseClass
{
//Rest of class properties/ctors/function definitions.
}
Additionally, I have set up a DbContext derived class using the fluent API in it in order to have Table per Type table mapping in the generated database. 另外,我使用流利的API设置了一个DbContext派生类,以便在生成的数据库中具有每个类型的表映射。
I know that the Entity Framework uses the class properties in order to generate the database table fields. 我知道实体框架使用类属性来生成数据库表字段。 Because of some work project requirements, I want to define some "old-style" encapsulation logic classes in C#, defining fields and accessors/modifiers without properties and to generate tables via the EF code-first approach using those classes, for example:
由于某些工作项目的要求,我想在C#中定义一些“旧式”封装逻辑类,定义没有属性的字段和访问器/修饰符,并使用这些类通过EF代码优先方法生成表,例如:
public BaseClass
{
private int _classID;
public void SetClassID(int classID) {_classID = classID;}
public int GetClassID() {return _classID;}
//Rest of class properties/ctors/function definitions.
}
So, I would like to know, is there any way, trick or workaround to set the Entity Framework work with it (maybe generate fields based on private fields and not properties)? 因此,我想知道,是否有任何方法,技巧或变通办法来设置实体框架(可能基于私有字段而不是属性生成字段)? My google search did not provide any relevant results.
我的Google搜索未提供任何相关结果。
Thank you in advance. 先感谢您。
The final solution I applied, was creating C# classes used from the Entity Framework containing C# properties, allowing the construction of C# objects using the tool-generated structures (same applies with classes): 我应用的最终解决方案是从包含C#属性的实体框架中创建C#类,从而允许使用工具生成的结构来构造C#对象(与类相同):
//Tool struct
struct AStruct{
int value;
};
//C# class used by EF:
public class A{
public int Value { get; set; }
//Rest of ctors....
public A(AStruct s)
{
Value = s.value;
}
public AStruct ToStruct(){
return new AStruct
{
value = Value;
}
}
//Rest of code here....
}
This did the trick, as EF uses pure C# classes, and I have "wrapped" in a sense the tool structures for EF. 因为EF使用纯C#类,所以做到了这一点,从某种意义上说,我已经“包装”了EF的工具结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.