none
WPF Application using EF 5 and SqlCe 4 - create, deploy and migrate RRS feed

  • Question

  • Hi,

    I have to create a distributed WPF application with a flat database on client side in order to work offline if the WCF service is not reachable. 

    I thought about using entity framework model first approach, mapped to a dummy sqlce-database file. Then I would customize the whole mapping thing the way that a user-related sqlce-database-file is created on client side executing a bunch of create-table commands for the first usage. Read and write would happen through Linq to Entity.  

    Later, with every release,  I would ship a sql-file which updates the existing user-database. 

    What I've got so far is the automatic generated .sqlce file when using "create database from model". I can modify this file, add it to the properties resouces of the assembly and execute the sql statements. I create a database file with user related filename, I can insert, update and delete using normal ado.net.

    What I don't get is the mapping to the entity framework context to use linq to entity instead of sql statements.

    I read about "SqlCeConnectionFactory" and the method SetInitializer() but I don't understand how to use it properly. Simply because I don't think that changing the automatically created context class that inherits from DbContext is the right way. 

    Can anybody give me some hints, advices, code snippets, links etc... that could solve my problem? 

    Maybe I am also totally wrong and there is a better way to get what I want?

    Thanks.

    Tuesday, August 27, 2013 10:26 AM

Answers

  • Hello faa-db,

    Welcome toMSDN Support Forum.

    From your description, you want to know how to query use linq in entity framework.

    If I have misunderstood, please let me know.

    I made a sample and please see the code below:

    using (S29DBContext db = new S29DBContext())
                {
                    var query = (from s in db.Students.Include("Courses")
                                 select s).FirstOrDefault<Student>();
                }

    The codes above will query the first record in table Student and will query the record in table Course that has association with Student.

    More information about entity framework linq query examples:

     http://msdn.microsoft.com/en-us/library/vstudio/bb399367(v=vs.100).aspx

    I look forward to hearing from you.

    Best Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, August 30, 2013 8:02 AM
    Moderator

All replies

  • Hi,

    Welcome to MSDN Forum!

    I have moved this thread to Entity Framework forum for better response.

    Thanks for your understanding.

    Best regards,
    Franklin


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, August 28, 2013 8:26 AM
  • Hello,

    It seems that you want to know how to use Linq to query the database with entity framework.

    There is link that shows how to use it:

    http://msdn.microsoft.com/en-us/library/gg696167%28v=vs.103%29.aspx

    Hope to help you.

    Wednesday, August 28, 2013 11:15 AM
  • Hello faa-db,

    Welcome toMSDN Support Forum.

    From your description, you want to know how to query use linq in entity framework.

    If I have misunderstood, please let me know.

    I made a sample and please see the code below:

    using (S29DBContext db = new S29DBContext())
                {
                    var query = (from s in db.Students.Include("Courses")
                                 select s).FirstOrDefault<Student>();
                }

    The codes above will query the first record in table Student and will query the record in table Course that has association with Student.

    More information about entity framework linq query examples:

     http://msdn.microsoft.com/en-us/library/vstudio/bb399367(v=vs.100).aspx

    I look forward to hearing from you.

    Best Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, August 30, 2013 8:02 AM
    Moderator
  • Sorry,

    I know how to use linq and how to query. The question was another one. I use the code-first approach now, whicht wasn' t my intention. But this seemed to be the only approach that has examples and helpful tipps in web. 

    public class XXXContextFactory : IDbContextFactory<XXXContext>
        {
            public bool CreateDbIfNotExists { private get; set; }
            public string Connectionstring { private get; set; }
    
            public XXXContextFactory()
            {
            }
    
            public XXXContext Create()
            {
                Contract.Requires(!string.IsNullOrWhiteSpace(this.Connectionstring));
    
                Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0", "", Connectionstring);
    
                if (CreateDbIfNotExists == false)
                {
                    Database.SetInitializer(new MigrateDatabaseToLatestVersion<XXXContext, XXXMigrationsConfiguration>());
                    return new XXXContext(Connectionstring);
                }
    
                Database.SetInitializer<XXXContext>(null);
                bool exists = Database.Exists(Connectionstring);
    
                if (!exists)
                {
                    Database.SetInitializer(new XXXCreateIfNotExistsInitializer());
                    XXXContext context = new XXXContext(Connectionstring);
                    context.Database.Initialize(true);
                    return context;
                }
    
                Database.SetInitializer(new MigrateDatabaseToLatestVersion<XXXContext, XXXMigrationsConfiguration>());
                return new XXXContext(Connectionstring);
            }

    This is how I get access to the context. But I'm not satisfied with this solution. It seems not to be right one as I always have to re-instantiate the DBContext.

    Monday, September 9, 2013 7:35 AM
  • Hi faa-db,

    Sorry for misunderstanding you.

    So you want to connect your database dynamicly. Is it Right?

    In my understanding, we can change context connection at runtime in both model-first and db-first like below:

    objectContext= New DataBaseFirstDBEntities( _
            "metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;" _
            & "provider=System.Data.SqlClient;" _
            & "provider connection string=" _
               & "'data source=.\SQLEXPRESS;attachdbfilename=""" & DBFileName & """;" _
               & "integrated security=True;connect timeout=30;user instance=True;" _
               & "multipleactiveresultsets=True;App=EntityFramework'")

    Then the context will be changed to connect database DBFileName.

    And if you have many database file, you can create a factory class to store various connections.

    Best Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, September 9, 2013 9:09 AM
    Moderator