c# mysql 代码生成器,代码生成-如何从C#类生成数据库表?

真的很晚,我只花了大约10分钟的时间,所以它非常草率,但是它确实起作用,并且会给您一个很好的起点:

using System;

using System.Collections.Generic;

using System.Text;

using System.Reflection;

namespace TableGenerator

{

class Program

{

static void Main(string[] args)

{

List tables = new List();

// Pass assembly name via argument

Assembly a = Assembly.LoadFile(args[0]);

Type[] types = a.GetTypes();

// Get Types in the assembly.

foreach (Type t in types)

{

TableClass tc = new TableClass(t);

tables.Add(tc);

}

// Create SQL for each table

foreach (TableClass table in tables)

{

Console.WriteLine(table.CreateTableScript());

Console.WriteLine();

}

// Total Hacked way to find FK relationships! Too lazy to fix right now

foreach (TableClass table in tables)

{

foreach (KeyValuePair field in table.Fields)

{

foreach (TableClass t2 in tables)

{

if (field.Value.Name == t2.ClassName)

{

// We have a FK Relationship!

Console.WriteLine("GO");

Console.WriteLine("ALTER TABLE " + table.ClassName + " WITH NOCHECK");

Console.WriteLine("ADD CONSTRAINT FK_" + field.Key + " FOREIGN KEY (" + field.Key + ") REFERENCES " + t2.ClassName + "(ID)");

Console.WriteLine("GO");

}

}

}

}

}

}

public class TableClass

{

private List> _fieldInfo = new List>();

private string _className = String.Empty;

private Dictionary dataMapper

{

get

{

// Add the rest of your CLR Types to SQL Types mapping here

Dictionary dataMapper = new Dictionary();

dataMapper.Add(typeof(int), "BIGINT");

dataMapper.Add(typeof(string), "NVARCHAR(500)");

dataMapper.Add(typeof(bool), "BIT");

dataMapper.Add(typeof(DateTime), "DATETIME");

dataMapper.Add(typeof(float), "FLOAT");

dataMapper.Add(typeof(decimal), "DECIMAL(18,0)");

dataMapper.Add(typeof(Guid), "UNIQUEIDENTIFIER");

return dataMapper;

}

}

public List> Fields

{

get { return this._fieldInfo; }

set { this._fieldInfo = value; }

}

public string ClassName

{

get { return this._className; }

set { this._className = value; }

}

public TableClass(Type t)

{

this._className = t.Name;

foreach (PropertyInfo p in t.GetProperties())

{

KeyValuePair field = new KeyValuePair(p.Name, p.PropertyType);

this.Fields.Add(field);

}

}

public string CreateTableScript()

{

System.Text.StringBuilder script = new StringBuilder();

script.AppendLine("CREATE TABLE " + this.ClassName);

script.AppendLine("(");

script.AppendLine("\t ID BIGINT,");

for (int i = 0; i < this.Fields.Count; i++)

{

KeyValuePair field = this.Fields[i];

if (dataMapper.ContainsKey(field.Value))

{

script.Append("\t " + field.Key + " " + dataMapper[field.Value]);

}

else

{

// Complex Type?

script.Append("\t " + field.Key + " BIGINT");

}

if (i != this.Fields.Count - 1)

{

script.Append(",");

}

script.Append(Environment.NewLine);

}

script.AppendLine(")");

return script.ToString();

}

}

}

我将这些类放在程序集中进行测试:

public class FakeDataClass

{

public int AnInt

{

get;

set;

}

public string AString

{

get;

set;

}

public float AFloat

{

get;

set;

}

public FKClass AFKReference

{

get;

set;

}

}

public class FKClass

{

public int AFKInt

{

get;

set;

}

}

它生成了以下SQL:

CREATE TABLE FakeDataClass

(

ID BIGINT,

AnInt BIGINT,

AString NVARCHAR(255),

AFloat FLOAT,

AFKReference BIGINT

)

CREATE TABLE FKClass

(

ID BIGINT,

AFKInt BIGINT

)

GO

ALTER TABLE FakeDataClass WITH NOCHECK

ADD CONSTRAINT FK_AFKReference FOREIGN KEY (AFKReference) REFERENCES FKClass(ID)

GO

一些进一步的想法...我会考虑在类中添加[SqlTable]之类的属性,这样它只会为所需的类生成表。 此外,这可以清理大量,修复错误,进行优化(FK Checker只是在开玩笑)等……只是为了让您入门。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值