How can I generate database tables from C# classes?

前端 未结 12 1459
谎友^
谎友^ 2020-12-02 05:26

Does anyone know a way to auto-generate database tables for a given class? I\'m not looking for an entire persistence layer - I already have a data access solution I\'m usi

12条回答
  •  隐瞒了意图╮
    2020-12-02 06:15

    For complex types, you can recursively convert each one that you come across into a table of its own and then attempt to manage foreign key relationships.

    You may also want to pre-specify which classes will or won't be converted to tables. As for complex data that you want reflected in the database without bloating the schema, you can have one or more tables for miscellaneous types. This example uses as many as 4:

    CREATE TABLE MiscTypes /* may have to include standard types as well */
     ( TypeID INT,
       TypeName VARCHAR(...)
     )
    
    CREATE TABLE MiscProperties
     ( PropertyID INT,
       DeclaringTypeID INT, /* FK to MiscTypes */
       PropertyName VARCHAR(...),
       ValueTypeID INT /* FK to MiscTypes */
     )
    
    CREATE TABLE MiscData
     (  ObjectID INT,
        TypeID  INT
     )
    
    CREATE TABLE MiscValues
     ( ObjectID INT, /* FK to MiscData*/
       PropertyID INT,
       Value VARCHAR(...)
     )
    

提交回复
热议问题