简体   繁体   English

是否可以在不使用Entity Framework中以代码优先方式使用C#属性的情况下生成数据库字段?

[英]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.

相关问题 如果我们使用具有代码优先方法的Entity框架,是否可以在给定路径上创建数据库(Sql Server compact)? - Is possible to create a database (Sql Server compact) on a given path if we use Entity framework with code-first approach? 是否可以使用代码优先实体框架在数据库上生成基本的CRUD存储过程? - Is it possible to generate basic CRUD stored procedures on database with code-first entity framework? 实体未使用“代码优先”方法更新 - Entity not updating using Code-First approach C#交互式窗口中的实体框架Code-First(Oracle) - Entity framework Code-First in c# interactive window (Oracle) C#-实体框架-大种子数据代码优先 - C# - Entity Framework - Large seed data code-first 将列添加到 C# 和实体框架代码中的表中,而不删除数据库内容 - Add column to table in C# & Entity Framework code-first without deleting DB content C# 实体框架代码优先 - 忽略数据库中缺失的列 - C# Entity Framework code-first - ignore missing columns in database 实体框架代码优先使用现有数据库时的好处 - Entity Framework Code-First Benefits when using existing database 无法使用实体框架代码优先使用凭据创建数据库? - Cannot Create Database with credentials using Entity Framework Code-First? 在现有MySQL数据库上使用实体框架4.1 Code-First - Using entity framework 4.1 Code-First on existing MySQL database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM