c#, Is it possible to define databinding from sqlite database to c# object typesave?

倾然丶 夕夏残阳落幕 提交于 2020-01-15 10:51:22

问题


I have the following class hirarchy:

public class A
{
   public string A_string;
   public int A_int;
   public double A_double;
   public B BInstance;
}

public class B
{
   public string B_string;
   public int B_int;
   public double B_double;
   public C CInstance;
}

public class C
{
   public string C_string;
   public int C_int;
   public double C_double;
}

and need a mapping to the following database values:

+--------------+-----------+--------------+---------------------+-------------------+----------------------+--------------------------------+-----+---------------------+
| Col1(string) | Col2(int) | Col3(double) | Col4(string)        | Col5(int)         | Col6(double)         | Col7(string)                   | ... | Col_without_mapping |
+--------------+-----------+--------------+---------------------+-------------------+----------------------+--------------------------------+-----+---------------------+
| A_string     | A_int     | A_double     | A.Instance.B_string | A.BInstance.B_int | A.BInstance.B_double | A.BInstance.CInstance.C_string | ... |                     |
+--------------+-----------+--------------+---------------------+-------------------+----------------------+--------------------------------+-----+---------------------+

The database has thousand of columns and the class hierarchy is very complex, therefore I do not want to rewrite the mapping (and the datatype casting (from the database to c# and vice versa)) for every function accessing the database. Ideally, I would like to define the databinding once if possible.

I know the entity framework, but there are only the options:

  1. code first
  2. database first

What I need is an databinding for an existing code to an existing database.

I read about Fluent API

maybe something like this would work:

modelBuilder.ComplexType<A>();
modelBuilder.ComplexType<B>();
modelBuilder.ComplexType<C>();

modelBuilder.Entity<A>().ToTable("databasetable_above"); 
modelBuilder.Entity<B>().ToTable("databasetable_above");
modelBuilder.Entity<C>().ToTable("databasetable_above");

But i do not know if it is possible. If it is, I would be glad for a code snippet showing the mapping. Or, if someone has a better idea I would be happy to hear.

EDIT:

Does anyone know the code, if I could replace the database with the Fluent API generated database.

Thank you

来源:https://stackoverflow.com/questions/50872322/c-is-it-possible-to-define-databinding-from-sqlite-database-to-c-sharp-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!