in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

Creating a new sqlite database with Entity Framework

Last post 04-29-2009 3:06 AM by youngfe. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 10-09-2008 5:15 AM

    • Amol
    • Top 500 Contributor
    • Joined on 10-08-2008
    • Posts 5

    Creating a new sqlite database with Entity Framework

    Hi,

    I am new to Entity Framework, and I am trying to create a new sqlite database. So I created the data model (edmx) file. Now how do I set the sqlite connection string that can talk to this edmx file? and how to do I create a new datacontext object that can create a new db? I have handled sqlite databases before, but never through entity framework.

    I looked at the textlinq sample, but (I think) in that the database was already created and added as an 'existing item' and the corresponding connection string was written in the application config file.

    Thanks.

     

  • 10-09-2008 8:17 AM In reply to

    Re: Creating a new sqlite database with Entity Framework

    Neither Entity Framework nor Visual Studio create databases from models ..... yet.

    Regards

    Jesús López

  • 10-09-2008 9:51 AM In reply to

    • Amol
    • Top 500 Contributor
    • Joined on 10-08-2008
    • Posts 5

    Re: Creating a new sqlite database with Entity Framework

    Thanks for the reply.

    So how do I do these (if there is a way to do so)

    - I can update the data model from the database, but can I do the reverse? i.e., if I make changes to the data model, e.g., add table, can I put those in the database?

    - How do I programatically switch the database?

     

    Amol

     

    I could get the datacontext variable, but did not know what to do with it to achieve the above.

    System.Data.SQLite.SQLiteConnection sq = new System.Data.SQLite.SQLiteConnection(@"Data Source=C:\dd.db;New=True;UTF8Encoding=True;Version=3");

    System.Data.Linq.DataContext c = new System.Data.Linq.DataContext(sq);

  • 10-10-2008 2:18 AM In reply to

    Re: Creating a new sqlite database with Entity Framework

    Amol:
    if I make changes to the data model, e.g., add table, can I put those in the database?

    You need to do it manually. I would suggest you to change the database first, then update your model from database.

    Amol:
    How do I programatically switch the database?

    You need to create another object context providing another connection string.

    Amol:

    System.Data.Linq.DataContext c = new System.Data.Linq.DataContext(sq);

    DataContext is part of LINQ to SQL, which only officially supports SQL Server. You need to use Entity Framework. System.Data.Objects.ObjectContext is the Entity Framework equivalent to System.Data.Linq.DataContext. When you add a "ADO.NET Entity Data Model" item to your project, Visual Studio generates a class that inherits form System.Data.Object.ObjectContext.

    Regards

    Jesús López

  • 10-10-2008 9:00 AM In reply to

    • Amol
    • Top 500 Contributor
    • Joined on 10-08-2008
    • Posts 5

    Re: Creating a new sqlite database with Entity Framework

    Thanks a lot !! This helps a lot.

    Just for  benefit for any other beginner (like me), here's how I did it:

     

    System.Data.EntityClient.
    EntityConnectionStringBuilder ee = new System.Data.EntityClient.EntityConnectionStringBuilder();

    ee.Provider = "System.Data.SQLite";

    ee.Metadata = @"res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl";

    ee.ProviderConnectionString = @"data source=C:\Mydata.db;Version=3;";

    System.Data.EntityClient.EntityConnection my_entity = new System.Data.EntityClient.EntityConnection(ee.ConnectionString);

     

    using (MydataEntities temp = new MydataEntities(my_entity))

    {

    }

  • 04-27-2009 11:27 AM In reply to

    • SC-A9
    • Top 50 Contributor
    • Joined on 10-22-2006
    • Posts 21

    Re: Creating a new sqlite database with Entity Framework

     Something is not working out 100% for me with this. Can anyone provide a .zip of working code or a tutorial web page? I've made a few web search queries and have not found a full example yet.

     

     

    Thanks.

     

  • 04-29-2009 3:06 AM In reply to

    Re: Creating a new sqlite database with Entity Framework

    I've made a class can easily create connect. Hope this can help you. Public Class iSqliteEntity Public Shared ReadOnly Property ConnectionString(ByVal Model As String, ByVal DataFilePath As String) Get Dim csBuilder As New System.Data.EntityClient.EntityConnectionStringBuilder csBuilder.Provider = "System.Data.SQLite" csBuilder.Metadata = "res://*/world.csdl|res://*/world.ssdl|res://*/world.msl".Replace("world", Model) csBuilder.ProviderConnectionString = "data source=" + DataFilePath + ";Version=3;" Return csBuilder.ConnectionString End Get End Property Public Shared ReadOnly Property Connect(ByVal Model As String, ByVal DataFilePath As String) As EntityClient.EntityConnection Get Return New EntityClient.EntityConnection(ConnectionString(Model, DataFilePath)) End Get End Property End Class
    Youngfe from ciico jewelry
Page 1 of 1 (7 items)
Powered by Community Server (Commercial Edition), by Telligent Systems