- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在创建一个相对较小的基于数据库的应用程序(约 20 个表,其中最大的最多 20K 行。我们将使用 SQLite,因为我们希望支持 Windows OS X、iPhone/iPad 和 Android并且需要轻松移动文件,因此 SQLite 绝对是正确的选择。
过去,我只是简单地使用了 SQLite 包装类,它们除了允许您将 SQL 作为操作或查询对数据库执行外,几乎什么都不做。一点也不花哨。
我想知道的是,我看到 Entity Framework 也支持 SQLite,至少在 Windows 方面是这样。我想知道使用它是否更有意义,或者我是否应该坚持使用相同的、简单的直接 SQL 模式,因为我基本上必须在其他平台上这样做。但与我交谈过的很多人一直在说 EF6.x 现在有多棒。
我还记得我们过去的 ADO.NET 数据集时代,我们喜欢数据集的可视化设计器来设置表、键、关系等。我希望能够使用它或类似的东西,但是我可以在下面的代码中使用 [Table] 和 [Column] 等属性。
想法?
我知道 SQLite 不支持 Code First,但我实际上开发了一个 SQLiteContextInitializer,它确实允许我在 Code-first 场景中做我需要的几乎所有事情。这是供引用/增强。我在网上找到了其他人的类似版本,并对其进行了增强以正确使用 TableName 属性等。享受吧!
class SqliteContextInitializer<T> : IDatabaseInitializer<T>
where T : SQLiteContext<T>
{
private readonly bool _dbExists;
private readonly DbModelBuilder _modelBuilder;
public delegate void BeforeCreateDatabaseDelegate(DbModel model, out bool autoGenerateSchema);
public readonly BeforeCreateDatabaseDelegate BeforeCreateDatabase;
public delegate void AfterCreateDatabaseDelegate(DbModel model);
public readonly AfterCreateDatabaseDelegate AfterCreateDatabase;
public SqliteContextInitializer(string dbPath, DbModelBuilder modelBuilder, BeforeCreateDatabaseDelegate beforeCreateDatabase = null, AfterCreateDatabaseDelegate afterCreateDatabase = null)
{
_dbExists = File.Exists(dbPath);
_modelBuilder = modelBuilder;
BeforeCreateDatabase = beforeCreateDatabase;
AfterCreateDatabase = afterCreateDatabase;
}
public void InitializeDatabase(T context)
{
if (_dbExists)
return;
var model = _modelBuilder.Build(context.Database.Connection);
using (var transaction = context.Database.BeginTransaction())
{
try
{
bool autoGenerateSchema = true;
if(BeforeCreateDatabase != null)
BeforeCreateDatabase(model, out autoGenerateSchema);
if (autoGenerateSchema)
{
AutoCreateSchema(context.Database, model);
if(AfterCreateDatabase != null)
AfterCreateDatabase(model);
}
transaction.Commit();
}
catch (Exception)
{
transaction.Rollback();
throw;
}
}
}
class Index
{
public string Name { get; set; }
public string Table { get; set; }
public List<string> Columns { get; set; }
}
private void AutoCreateSchema(Database db, DbModel model)
{
const string tableTemplate = "CREATE TABLE [{0}] (\n{1}\n);";
const string columnTemplate = " [{0}] {1} {2}"; // name, type, decl
const string primaryKeyTemplate = " PRIMARY KEY ({0})";
const string foreignKeyTemplate = " FOREIGN KEY ({0}) REFERENCES {1} ({2})";
const string indexTemplate = "CREATE INDEX {0} ON {1} ({2});";
var indicies = new Dictionary<string, Index>();
foreach (var tableInfo in model.StoreModel.EntityTypes)
{
var tableDefinitionRecords = new List<string>();
// columns
foreach (var columnInfo in tableInfo.Properties)
{
var columnDeclarations = new HashSet<string>();
if (!columnInfo.Nullable)
columnDeclarations.Add("NOT NULL");
var indexAnnotations = columnInfo.MetadataProperties
.Select(x => x.Value)
.OfType<IndexAnnotation>();
foreach (var indexAnnotation in indexAnnotations)
{
foreach (var indexAttribute in indexAnnotation.Indexes)
{
if (indexAttribute.IsUnique)
columnDeclarations.Add("UNIQUE");
if (string.IsNullOrEmpty(indexAttribute.Name))
continue;
Index index;
if (!indicies.TryGetValue(indexAttribute.Name, out index))
{
index = new Index
{
Name = indexAttribute.Name,
Table = tableInfo.Name,
Columns = new List<string>(),
};
indicies.Add(index.Name, index);
}
index.Columns.Add(columnInfo.Name);
}
}
// Add column definition record
var columnDefinitionRecord = string.Format(columnTemplate,
columnInfo.Name, // Unlike with tables, this picks up the attribute-specified value
columnInfo.TypeName,
string.Join(" ", columnDeclarations));
tableDefinitionRecords.Add(columnDefinitionRecord);
}
// primary keys
if (tableInfo.KeyProperties.Any()) // then add primary key definition record
{
var primaryKeyColumnNames = tableInfo.KeyProperties.Select(x => x.Name);
var primaryKeyRecord = string.Format(primaryKeyTemplate,
string.Join(", ", primaryKeyColumnNames));
tableDefinitionRecords.Add(primaryKeyRecord);
}
// foreign keys
foreach (var assoc in model.StoreModel.AssociationTypes)
{
if (assoc.Constraint.ToRole.Name == tableInfo.Name)
{
var toKeyColumnNames = assoc.Constraint.ToProperties.Select(x => x.Name);
var fromKeyColumnNames = assoc.Constraint.FromProperties.Select(x => x.Name);
// Add foreign key definition record
var foreignKeyDefinitionRecord = string.Format(foreignKeyTemplate,
string.Join(", ", toKeyColumnNames),
assoc.Constraint.FromRole.Name,
string.Join(", ", fromKeyColumnNames));
tableDefinitionRecords.Add(foreignKeyDefinitionRecord);
}
}
MetadataProperty tableNameProperty;
var tableName = tableInfo.MetadataProperties.TryGetValue("TableName", true, out tableNameProperty)
? tableNameProperty.Value
: tableInfo.Name;
// Create table
var sql = string.Format(tableTemplate,
tableName,
string.Join(",\n", tableDefinitionRecords));
db.ExecuteSqlCommand(sql);
}
// create indexes for all tables
foreach (var index in indicies.Values)
{
var columns = string.Join(", ", index.Columns);
var sql = string.Format(indexTemplate,
index.Name,
index.Table,
columns);
db.ExecuteSqlCommand(sql);
}
}
}
最佳答案
使用 ORM 是有意义的。问题是哪一个?
ORM 的好处:
关于c# - Entity Framework 对较小的桌面应用程序有意义吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30419057/
我有这些实体(这只是我为这篇文章创建的抽象): 语言 区 说明 这些是它们之间的引用: 区 * - 1 语言 说明 * - 1 语言 区 1 - 1 说明 如果我这样取: var myFetch =
经过大量谷歌搜索后,除了降级 hibernate 版本之外,我没有找到问题的答案。但我在 2003 年类似的帖子中遇到了这种情况。 问题是什么: //in the first session I d
我听说过 linq to entities 。 Entity Framework 是利用linq to entities吗? 最佳答案 LINQ to Entities 是 Entity Framew
我是 Entity Framework 和 ASP.Net MVC 的新手,主要从教程中学习,对任何一个都没有深入了解。 (我确实有 .Net 2.0、ADO.Net 和 WebForms 方面的经验
如果我编写 LINQ to Entities 查询,该查询是否会转换为提供程序理解的 native 查询(即 SqlClient)? 或者 它是否会转换为实体 SQL,然后 Entity Framew
这个问题已经有答案了: EF: Include with where clause [duplicate] (5 个回答) 已关闭 2 年前。 看来我无法从数据库中获取父级及其子级的子集。 例如...
我开始在一家新公司工作,我必须在一个旧项目上使用 C++ 工作。所以,我忘记了一些 C++ 本身的代码结构。在一个函数中,我在一个函数中有双冒号::,但我不知道如何理解它。 例如,我知道如果我有 EN
我写了一个方法来允许为 orderby 子句传递一个表达式,但我遇到了这个问题。 Unable to cast the type 'System.DateTime' to type 'System.I
简单的问题:LINQ to Entities 和 Entity Framework 有什么区别?到目前为止,我认为这两个名称是用来描述同一个查询的,但我开始觉得事实并非如此。 最佳答案 Entity
我想使用 Entity Framework 。但是,我还要求允许我的用户在我们的系统中定义自定义字段。我想仍然使用 Entity Framework ,而不是使用具有哈希表属性的分部类。 下面是我想到
我正在阅读这个 E.F. 团队博客的这个系列 http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-featu
我正在使用 EF6 开发插件应用程序,代码优先。 我有一个名为 User 的实体的主要上下文。 : public class MainDataContext : DbContext { pub
当我得到最后的 .edmx 时,我遇到了问题。 我收到一条消息说 错误 11007:未映射实体类型“pl_Micro”。 查看设计器 View ,我确实看到该表确实存在。 我怎样才能克服这个消息? 最
我已阅读与使用 Entity Framework 时在 Linq to Entities (.NET 3.5) 中实现等效的 LEFT OUTER JOIN 相关的所有帖子,但尚未找到解决以下问题的方
使用 WCF RIA 服务和 Entity Framework 4. 我有 3 个 DTO:学校、州、区。 州 DTO 有一个地区属性(property),其构成。学校 DTO 有一个国家属性(pro
我有一个 Employee 实体,它继承自一个继承自 Resource 实体(Employee -> Person -> Resource)的 Person 实体。是否可以通过编程方式获取 Emplo
我有一个使用 JPA 的 java 应用程序。 假设我有一个名为 Product 的实体与 name和 price属性(所有实体都有一个 id 属性)。 自然我可以得到一个List相当容易(来自查询或
我有一个 Entity Framework 类,其中有两个指向另一个对象的引用 public class Review { [Key] public int Id {get;s
我是 Symfony 2 的新手,我想知道一些事情: 假设我的项目中有 2 个 bundle 。我想在两个包中使用从我的数据库生成的实体。 我应该在哪里生成实体? (对我来说,最好的方法是在 bund
我想在具有方法和属性的部分类中扩展 EF 实体。我经常这样做。 但是现在我需要将来自该实体的数据与来自其他实体的数据结合起来。因此,我需要能够访问实体 objectcontext(如果附加)来进行这些
我是一名优秀的程序员,十分优秀!