VS2010使用EntityFramework操作Sqlite数据库

开关电源EMI超标?抖频(展频)技术实战解析 开关电源的电磁干扰(EMI)是电源设计中的常见难题,尤其在传导发射测试中,固定开关频率产生的离散谐波尖峰容易超出限值。针对此类窄带干扰,抖频(展频)技术提供了一种无需增加元器件的解决方案:通过周期性改变开关频率,将原本集中在单一频点上的能量扩散到更宽频带,从而显著降低峰值幅度。该技术对150kHz至1MHz频段的谐波超标尤其有效,实测中可带来约10dB的峰值改善,是PCB布局与滤波器整改之外的重要补充手段。本文从噪声产生机理出发,介绍展频调制参数的选择依据,结合65kHz反激电源的实测数据,并探讨抖频与滤波 阅读详情

http://www.codeproject.com/Articles/236918/Using-SQLite-embedded-database-with-entity-framewo


http://www.cnblogs.com/walkingboy/archive/2009/10/27/linqtosqlite.html


Table of Contents

  • Introduction
  • Adding the provider
  • SQLite management tool
  • Edit SQLite database using Visual Studio 2010 or SQLite Administrator
  • Using SQLite with Entity Framework in Visual Studio 2010
  • Task management example with SQLite
  • Summary

Introduction

Every now and then, we need a simple database solution which is light, small and pretty easy to use. What more can be useful other than SQLite, a simple yet very powerful tool to support database for any kind of application needs. When we started this research work, our major concern was to choose the right kind of database depending on few criteria.

  • We don’t want to use a service based database rather a file system based database.
  • Clients have to be light, fast and support blob data.
  • We don’t want to install any client or framework in client machine.

SQLite is turning to be a very handy embedded database as far as the web and small scale database is concerned, and some people prefer SQLite over SQL compact Edition database because of its size, performance and support for large data. After doing research on few area of the database, I have decided to put together the learnings that I had while exploring the uses and ease while working with Visual Studio 2010. In this short article, we are going to see how we can use ADO.NET entity framework and then how to use Linq to SQL with SQLite database.

Adding the Provider

The next step is to add a data connection in server explorer in Visual Studio 2010. When you click on add connection, by default, you don’t have provider added in Visual Studio, so we have to add it using a third-party tool. You can download the provider for SQLite from http://sqlite.phxsoftware.com/ site. “SQLite-1.0.66.0-setup” is the version we used for this particular case. When you install this provider, you would be given an option to integrate this in VS2008 and VS2010. After installing the provider when you get back to the add data connection in server explorer, you would see a new provider is available for SQLite.

SQLite_Provider_thumb_1_.jpg

Adding the Connection in Server Explorer

Go ahead and click on after choosing SQLite Database File. Next we will be presented with a screen, where you would either choose an existing database or create a new database file.

That’s it! Your connection string and database connection is now set up and you are good to go with the design of the database. Few interesting notes on this particular case. SQLite database has no database diagram support yet, but we hope that this will be provided soon. After adding connection, we can created tables and add necessary relations to the table.

In this section, we have seen how easily SQLite can be used in Visual Studio 2010 using a provider to create connection in server explorer. Next we would discuss how to use SQLite to create a data layer using Entity Framework, but before that let's take a quick look at a simple tool for SQLite DB management.

SQLite Management Tool

SQLite has lots of management tools, some of which are open source, some are trial ware and others are commercial. I found “SQLite Administrator” pretty easy to use and handy tool as far as SQLite database file is concerned.

A complete list of SQLite management tools can be found at “http://sqlite.com/cvstrac/wiki?p=ManagementTools”. Download whatever best suits you.

Here, I am going to talk about only SQLite Administrator. It can be downloaded from the following site:

Edit_Table_in_SQLite_Administrator.PNG

Note

This tool needs no installations whatsoever. The whole package is in a zip file and needs zero installation, just unzip and open the EXE, that’s it, and all of the functionality is pretty easy and it has a very intuitive UI. I would recommend it to everyone who works with SQLite.

Edit SQLite Database using Visual Studio 2010 or SQLite Administrator

In the previous section, I have shared little useful information about SQLite Embedded database and how to use its provider to create a connection using System.Data.SQLite. We have also seen a simple tool named “SQLite Administrator” which is very handy to use with SQLite database. In this short article, we will see how we can create and manipulate data using Visual Studio 2010 or SQLite Administrator.

Creating Table in Visual Studio 2010

Select the SQLite connection that you created and expand the Tables node, then right click and select “Add New Table”, that’s it. You are good to go to add a table just like the below figure. I am sure you guys have done this hundreds of time in SQL server, this time it's just SQLite. You might see a warning when you first enter into edit of table editor.

Using SQLite Administrator

Double click on the SQLite database file, than right click on tables and click "Create Table”, rest of the UI is fanatic to Create the table. Set field as primary key or assign auto increment to primary key, all can be done via simple clicks. So no custom hand written SQL any more.

Creating Relations in Visual Studio 2010

Creating the table relations are easy in Visual Studio, just click on relation icon, the icon beside primary key, in fact to be precise right side icon of primary key button. Or perhaps you can select a table and then click on relations, this will pop-up an widget to create relation.

Creating auto increment primary key is not that much difficult also, now a days in every table we use auto increment key so we must discuss about it. At first glance, we didn’t find any button or option to assign it directly. You can use SQLite Administrator to add it in few clicks, but to add in Visual Studio 2010 you got to bring the indexes window, just click on 'Indexes' after that you will find an option to add auto increment column.

AddRelation_thumb_1_.png

Using SQLite with Entity Framework in Visual Studio 2010

In the previous section, we have seen how using a simple “Data provider” provided byhttp://sqlite.phxsoftware.com/ can make our life easier in the context of SQLite in .NET. In this section, we are going to see how we can use SQLite to generate Entity Framework Data model.

Choosing the Provider

The primary thing is to first create any type of project in Visual Studio 2010, but I would prefer a web application or Windows Forms or perhaps a WPF application. Now click on 'Project' and select 'add new' item to add a new item type. From Add new item wizard, select “ADO.NET Entity Data Model” and click on add, this will bring up the Entity data model wizard.

  • Step 1: Select Generate from Database.
  • Step 2: In next window, select New connection.
  • Step 3: Connection properties window will popup and then we have to select the right kind of provider for SQLite.
  • Step 4: After that, the Connection properties will have browser or New db.
  • Step 5: Now select existing db if you have using browser or New to create new db.
  • Step 6: Continue with the wizard to select database table items to include in model.
  • Step 7: Click finish to close the wizard and a data model will be generated for you.

Data_Model_Created_thumb_4_.png

If you had any relationship in the database, those will automatically be synchronized in data model, that’s it. We are good to jump in the code to retrieve data and do all types of necessary stuff that we must do in a database. Best of luck and happy coding.

Using LINQ-to-SQL with SQLite Database

In the previous section, we have discussed about how to use ADO.NET Entity Framework with SQLite database. And we have witnessed that it's almost as similar to SQL server in context of ease and usability when you have the right provider.

Today we are going to see how we can use Linq-to-SQL with SQLite database. We always looks for ways that would make our life easy and most important no more re-inventing the wheel. By default, you cannot drag and drop tables from server explorer or from data connections. It's because we don’t have any built in provider that supports Linq and if you try to do so, you would certainly encounter with the following error:

AddRelation.png

Now, I know what am I going to show you won't please you but it certainly serves our purpose to use Linq with SQLite. There is no magic or rocket engineering behind this, we have a DBLinq open source distribution that comes with various sorts of database support. For this particular case, let's stick to SQLite. The binaries can be found here, http://code.google.com/p/dblinq2007/, please download latest binaries from this site.

Now it's time to generate the OurDatabase.dbml file. The steps are not that much complex, Locate the DBLinq package that you downloaded from Google code, then unzip in a suitable folder location where you can access it easily.

Note that you need to copy the System.Data.SQLite DLL in the DBLinq directory.

Commands.png

Step 1: Create the dbml file:

DbMetal /provider:Sqlite /conn "Data Source=File.db3" /dbml:File.dbml

Step 2: Create code file for the dbml file:

DbMetal /code:File.cs File.dbml

Copy those files to the Visual Studio. That’s it! You are good to go.

Generated_DBML.PNG

Make sure that everything compiles okay by hitting build. Below I have put a simple code snippet to demonstrate how to use this dbml to retrieve data.

public static void GetData()
    {
        string ConStr = "Data Source=" + 
	  HttpContext.Current.Server.MapPath("App_Data\\PersonalExpenseDB.s3db") +
                        ";Version=3;";

        var connection = new SQLiteConnection(
            ConStr
            );

        connection.Open();

        var db = new Main(connection, new SqliteVendor());

        var users = db.User;

        var item = users.Take(10).ToList();
    }

Task Management Example with SQLite

In this section, I have put together a simple task editing code having only two field titles and description. This is just to demonstrate the idea of how we can use the above ideas in real coding for this particular example I have used ADO.NET entity framework. Below the code snippets are given for most important methods:

//load task
private void LoadTasks()
{
    var db = new PersonalExpenseDBEntities();
    var tasks = db.Tasks.ToList();
    RepeaterTasks.DataSource = tasks;
    RepeaterTasks.DataBind();
}

//add new task
private void AddNewTask()
{
    var db = new PersonalExpenseDBEntities();
    Task task;
    if (EditTaskId > 0)
    {
        task = db.Tasks.Where(p => p.ID == EditTaskId).SingleOrDefault();
        if (task != null)
        {
            task.Description = TextBoxDescription.Text.Trim();
            task.Title = TextBoxTitle.Text.Trim();
        }
        EditTaskId = 0;
    }
    else
    {
        task = new Task {Description = TextBoxDescription.Text.Trim(), 
        		Title = TextBoxTitle.Text.Trim()};
        db.AddToTasks(task);
    }
    db.SaveChanges(true);
}

//delete task
private static void DeleteTask(CommandEventArgs e)
{
    var db = new PersonalExpenseDBEntities();
    var argument = e.CommandArgument;
    int taskId = Convert.ToInt32(argument);
    var editingTask = db.Tasks.Where(p => p.ID == taskId).SingleOrDefault();
    db.DeleteObject(editingTask);
    db.SaveChanges();
}

//populate task
private void PopulateEditingTask(CommandEventArgs e)
{
    var argument = e.CommandArgument;
    int taskId = Convert.ToInt32(argument);
    var db = new PersonalExpenseDBEntities();
    var editingTask = db.Tasks.Where(p => p.ID == taskId).SingleOrDefault();
    if (editingTask != null)
    {
        TextBoxTitle.Text = editingTask.Title;
        TextBoxDescription.Text = editingTask.Description;
    }
    EditTaskId = taskId;
}

Note that while creating the connection string using the wizard, you might end up with database path that is not relative to your application. So please modify the connection string so that it takes relative location fromapp_data.

Summary

In this short article, we have seen how to administer SQLite database with various tools. and how we can use some useful tools to get it working with Linq-to-SQL. We have also seen how we can use provider to get some native support with Visual Studio 2010 to design and manage SQLite database. Lastly, we have seen how to use SQLite with entity framework to perform some operations on database.


VS2010环境下SQLite与Entity Framework的集成开发实践 随着技术的发展,本地数据库因其离线可用、轻量级和部署的便捷性而成为开发者的首选。在Visual Studio 2010(VS2010)环境中,集成SQLite和Entity Framework(EF)为开发人员提供了一种高效且功能强大的数据持久化解决方案。SQLite作为一个无服务器、零配置的SQL数据库引擎,因其对多种编程语言的广泛支持、轻量级部署,以及不需要单独管理的特性,使其成为嵌入式数据库的理想选择。 阅读详情

相关推荐

安时积分法和开路电压法结合估算锂电池SOC

基于安时积分法和开路电压法结合的锂电池SOC估计Simulink仿真建模,利用安时积分法计算放电过程中的SOC,在搁置状态利用开路电压法进行SOC修正。

qq_39304296的博客 2567

以正确的顺序在EntityFrameworkCore迁移中执行SQL代码(SQLite)

在本技巧中,您将看到一个新的MigrationSqlGenerator,它将从具有“ExecuteAfter”或“ExecuteBefore”属性的MigrationOperations生成SQL。

寒冰屋的专栏 631

SAP LSMW保姆级操作文档

SAP LSMW(Legacy System Migration Workbench)是一个强大的工具,旨在简化旧系统数据向SAP系统的迁移过程。它允许用户通过多种方式导入数据,包括直接输入、批处理输入和BAPI。LSMW支持数据映射和转换,确保数据在迁移过程中保持一致性,减少手动输入的需求,从而提高效率,适用于新系统实施和数据更新项目。在日常运维过程中我们经常会遇到用户要求我们批量改一写数据,通常这些字段的信息有些SAP是提供程序可以批量进行更改的比如主数据的MM17、通用更改数据的MASS。

我小时候很黑的博客 4985

使用identity+jwt保护你的webapi(一)——identity基础配置

目录 前言 Web API中集成Identity 安装相关包 自定义User,Context 数据库迁移 修改主键类型/表名 最后 参考资料 前言 用户模块几乎是每个系统必备的基础功能,如果每次开发一个新项目时都要做个用户模块,确实非常无聊。好在asp.net core给我们提供了Identity,使用起来也是比较方便,如果对用户这块需求不是非常个性化的话,identity是一个不错的选择。 ASP.NET Core Identity: 是一个 API,它支持用户 登录功.

寒冰屋的专栏 707

MC1496乘法器AM调制电路搭建全流程(附Multisim 14.0仿真文件)

本文提供了基于MC1496乘法器搭建AM调制电路的完整实战指南。针对仿真模型缺失的痛点,详细讲解了从晶体管级手动构建MC1496等效模型,到外围电路设计、调制度参数计算与调节,并在Multisim 14.0中进行仿真验证的全流程。通过深度剖析MC1496内部原理与AM信号调制机制,帮助读者获得从仿真到实践的可控设计与排错能力。

weixin_29190169的博客 606

T分布和T检验的理解,Python代码实现T检验的计算

每天学习一点,每天进步一点。 声明:本人所有的原创,都是自己在学习过程中的记录点滴,不一定都是对的,肯定也会有一些错误的想法。所以大家看一看就好,不可尽信。当然也欢迎指出。 T分布 定义,有来自标准正态分布的样本X ~ N(0,1),和来自卡方n分布的Y ~ X2(n), 那么有 Z= X/√(Y/n) 成为符合自由度为n的T分布 我们用Python代码画一下他的图形 # 给一个自由度,返回这个自由度的卡方分布 X²(n) def product(n): n = np.ceil(n).astype

PY_smallH的博客 8492

深入探索EF Core:.NET平台最强ORM框架全面解析

深入探索EF Core:.NET平台最强ORM框架全面解析 【免费下载链接】efcore efcore: 是 .NET 平台上一个开源的对象关系映射(ORM)框架,用于操作关系型数据库。适合开发者使用 .NET 进行数据库操作,简化数据访问和持久化过程。 ...

gitblog_00018的博客 617

Halcon异常检测训练避坑指南:如何避免GPU内存溢出和样本不足问题

本文针对Halcon深度学习异常检测训练中常见的GPU内存溢出和样本不足两大难题,提供了从原理分析到实战解决方案的深度指南。通过调整批次大小、图像尺寸、使用梯度累积等技巧有效管理显存,并利用针对性数据增强与人工合成策略破解小样本困境,帮助开发者构建稳定高效的训练流程。

river的博客 222

C#演示在.net core的Winform窗体下接入EF

本文主题:如果在.net core的窗体应用程序Winform环境下,以DB First的方式接入.net core的EntityFramework。 关于Code First的的方式,我之前有写过一个本地加载Sqlite的文章里面有,估计大部人也不知道就是了,那篇文章总共也才几个人阅读。 一、创建项目 本文针对.net core,所以创建Windows Forms App(.NET Core),然后创建1个.net standard的类库,命名为Parctice.Database 二、引入

C#开发 4398

4D毫米波雷达:高度信息与微多普勒效应

4D毫米波雷达:高度信息与微多普勒效应​ ,人工智能,计算机视觉,大模型,AI,本文深入解析4D毫米波雷达的高度信息获取与微多普勒效应。4D毫米波雷达通过MIMO技术、垂直天线布局及相位差计算等获取高度信息,相较传统雷达更精准。微多普勒效应由目标微运动引发,可用于目标识别、分类和运动监测。文中结合自动驾驶、安防监控案例,分析技术挑战与未来趋势,如信号干扰、数据处理复杂,未来将向高精度、与其他传感器融合发展,展现出在多领域的巨大应用潜力。

不踩坑,不知道坑有多深;不总结,踩过的坑白踩。 9万+

Vision 机器视觉案例 之 零件边缘缺陷检测

缺陷检测工具

机器视觉李小白的博客 1393

C# .Net ef(Entity Framework 6) SQLite配置使用(codefirst)

C# .NET 中使用 Entity Framework 6 配置和使用 SQLite 数据库(Code First 方式)可以通过以下步骤实现。这个过程包括安装必要的包、配置连接字符串、创建模型和上下文类,以及执行迁移。在 App.config 或 Web.config 文件中添加 SQLite 的连接字符串。// 查询并输出所有 Person。// 创建新 Person 对象。// 添加到上下文中。// 禁用默认的复数表名。1)安装必要的 NuGet 包。3)创建模型和上下文类。

weixin_42098295的博客 1731

电路设计仿真软件:Mentor Graphics_(1).MentorGraphics电路设计仿真软件概述

菜单栏:包含文件、编辑、视图、工具等菜单。工具栏:提供常用的工具按钮,如打开、保存、仿真等。原理图编辑区:用于绘制电路原理图。元件库:包含各种电路元件,支持拖拽操作。属性面板:显示选中元件的属性,可以进行参数设置。仿真控制区:用于设置仿真参数和控制仿真过程。输出窗口:显示仿真结果和日志信息。打开模型编辑器:点击“工具”菜单,选择“模型编辑器”。选择模型类型:在模型编辑器中选择模型类型,如SPICE模型。输入模型参数:根据需要输入模型的参数,如电阻值、电容值等。保存模型。

kkchenjj的博客 1933

VS2010使用Entity FrameWork操作Sqlite数据库

这几天在研究如何使用Entity Framework 访问数据库,其中也走了不少弯路,还好最后搭建环境成功,下面就详细说明下环境搭建步骤 1.下载安装SQLite Data Provider 至官网:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki下载版本sqlite-netFx40-setup-bun...

weixin_30670965的博客 123

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

基于 SQLite 数据库的 C# 实体模型的实现

在 C# 中使用实体模型(Entity Framework)一般使用 MSSQL 数据库,这也是 vs2010 的默认方式。 SQLite 作为一种轻型数据库,实现了 SQL92 标准的大部分规定,利用 .net 连接库,可以与实体模型配合使用。以下是需要准备的环境: Vs2010 + .Net Framework 4 System.Data.SQLite,下载地址。开发环境需...

aoyu3722的博客 626

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

把System.Data.SQLite和ADO.NET(WCF) Data Service双剑合璧

关于System.Data.SQLite,可以参见我之前的介绍博客。不过现在的最新版本1.0.65(2009年7月26日发布),相对之前的版本提供了如下新特性: 包装了sqlite 3.6.16 支持.NET 3.5 SP1 支持Mono 支持VS2008(其实如果你安装了2010的话,也支持的)设计器:数据库管理器,Typed DataSet(类型化数据集)设计器...

weixin_33717117的博客 151

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

可用于Asp.net的嵌入式数据库SqlCE4.0试用笔记

随着SQL Server Compact Edition4.0(SQL CE)对多线程的支持,使它不再只适用于运行在掌上电脑等手持设备中,而可以作为asp.net网站的轻量级数据库引擎使用。 Visual Studio 2010 或 Visual Web Developer 2010 Express 项目中添加新项“SQL Server Compact 4.0本地数据库”,就可以得到一个不需要特...

weixin_34183910的博客 208

Entity Framework4.2 (十)CodeFirst(EF4 .2的Code First方法)

长假归来,祝大家:龙年吉祥,龙马精神;福旺财旺事业旺,旺上加旺!! 差不多一个月没登录博客园了,呵呵。非常抱歉啦。 好了,闲言少叙,书归正传。呵呵。 ================================================== 前面我有写过一篇《Entity Framework4.0 (三)概述(EF4 的Code First方法)》,目的是为了演示一下CodeFi...

weixin_30878361的博客 142

了解EF CodeFirst的Migrator功能与Migrator.Net对比

在上一篇【数据库迁移利器:Migrator.Net】中,很多朋友提到了EF的CodeFirst也有数据库的迁移功能,说来真惭愧,玩了那么多年,至今还未去了解EF,今天来了解下CodeFirst然后与Migrator.Net进行下横向对比。 CodeFirst是EF提供的一种技术手段,使我们从以往的创建数据库后再创建模型变成了只需要关注代码方式进行创模,然后通过EF框架映射到数据库架构并生成。 Co...

weixin_45121123的博客 211

Navi.Soft31.WebMVC框架(含示例地址)

1概述 1.1应用场景 互联网高速发展,互联网软件也随之越来越多,Web程序越来越被广泛使用.它部署简单,维护方便,深得众多软件公司使用 Bootstrap前端框架,是最近非常流行的框架之一.它简洁,直观,强悍,移动设备优先的原则,让Web开发更迅速和简单 本产品使用微软推荐的MVC开发模式,加上Bootstrap前端组件,开发出一套数据库管理应用类软件的基础框架.包括:系统选项,功能模块...

weixin_30695195的博客 87

从事软件开发工作5年后的总结

.NET是个很大、很宽、很广的概念。一个.NET,牵涉了太多的内容。这里我将结合自己的学习和工作经验,大致的介绍一下.NET技术究竟包括哪些内容,当然还包括了与.NET开发相关的其它技术。 一、.NET Framework版本 2000年6月,微软宣布了其.NET战略,推出了全新的.NET平台C#语言。.NET Framework从1.0开始,到2.0版本,这是一个里程碑。现在用的最...

diaobiao8107的博客 272
上一篇: Windows下Python开发环境搭建及示例
下一篇: lucene nutch solr及hadoop的区别和联系
whilelie
博客等级 码龄16年 24粉丝 87原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值