SQLite.Net是一个ORM。如果使用它(Sqlite.cs),它不会重新创建表。
“在数据库中执行”创建表(如果不存在)“表示方法摘要。当你看到代码时,如果执行失败,则count将为0,并且代码从现有的ORM映射传递索引创建。
它只是看起来ORM映射,如果它存在的行为不同,如下所示。
它也为ORM创建索引,因为您知道我们应该使用[PrimaryKey]的。通过这种方式调用CRUD操作。
/// <summary>
/// Executes a "create table if not exists" on the database. It also
/// creates any specified indexes on the columns of the table. It uses
/// a schema automatically generated from the specified type. You can
/// later access this schema by calling GetMapping.
/// </summary>
/// <returns>
/// The number of entries added to the database schema.
/// </returns>
public int CreateTable<T>()
{
var ty = typeof(T);
if (_tables == null) {
_tables = new Dictionary<string, TableMapping>();
}
TableMapping map;
if (!_tables.TryGetValue (ty.FullName, out map)) {
map = GetMapping (ty);
_tables.Add (ty.FullName, map);
}
var query = "create table \"" + map.TableName + "\"(\n";
var decls = map.Columns.Select (p => Orm.SqlDecl (p));
var decl = string.Join (",\n", decls.ToArray());
query += decl;
query += ")";
var count = 0;
try {
Execute (query);
count = 1;
}
catch (SQLiteException) {
}
if (count == 0) {
// Table already exists, migrate it
MigrateTable (map);
}
foreach (var p in map.Columns.Where (x => x.IsIndexed)) {
var indexName = map.TableName + "_" + p.Name;
var q = string.Format ("create index if not exists \"{0}\" on \"{1}\"(\"{2}\")", indexName, map.TableName, p.Name);
count += Execute (q);
}
return count;
}
非常清晰和简单的解释。谢谢 – BRogers 2013-02-14 17:44:41