Entity Framework Code first for SQL Compact

Source: Internet
Author: User
Tags compact management studio sql server management sql server management studio connectionstrings

This blog will show you how to use the SQL Compact in EF Code first. The SQL Compact is a free relational database launched by Microsoft, and the latest version is the SQL Compact 4.0. Microsoft's development tools vs 2010/sql Server 2008 and subsequent versions do not have SQL Compac 3.5/4.0t database viewing tools. But there's a sqlcetoolbox tool on CodePlex that's pretty good. Kind of like a lite version of SQL Server Management Studio.

The SQL compact can be used in some of the following scenarios, such as windowphone applications, desktop programs, and, of course, SQLite. Here's an example of how to build the SQL Compact database using EF Code first.

1. Referencing the entity Framework for SQL Compact related DLLs in the program, you can:pm> install-package entityframework.sqlservercompact via NuGet, command ( Note If you need to generate the SQL Compact 3.5,nuget command: Install-package EntityFramework.SqlServerCompact.Legacy)

2. Project structure: The Database entity class (Actor.cs/filmactorrole.cs/filmgenere.cs/filmtitle.cs/producer.cs/role.cs) is stored in the models, Context in which the database contexts (DvdContext.cs) are stored. Because it's a demo, put these under one project. In real projects, it is recommended to put Models/context in different class libraries, so that it is easy to maintain and the structure of the program is clearer.

The following shows the Filmactorrole class, which describes the model class in a way that uses entityframework dataannotations.

    [Table ("Filmactorrole")]    public class Filmactorrole    {        [Key] public        int Id {get; set;}        [Required]        public int Filmtitleid {get; set;}        [ForeignKey ("Filmtitleid")]        Public Filmtitle Filetitle {get; set;}        [Required]        public int Actorid {get; set;}        [ForeignKey ("Actorid")]        Public actor actor {get; set;}        [Required]        public int Roleid {get; set;}        [ForeignKey ("Roleid")]        Public role role {get; set;}        [Required]        [Stringlength (255)]        public string Character {get; set;}        [Required]        [Stringlength (1024x768)]        public string Description {get; set;}    }

For the contents of EntityFramework DataAnnotations, please refer to: https://msdn.microsoft.com/en-us/data/jj591583.aspx

DvdContext.cs

    public class Dvdcontext:dbcontext    {public        dbset<actor> actors{get; set;}        Public dbset<filmactorrole> filmactorroles {get; set;}        Public dbset<filmgenere> filmgeneres {get; set;}        Public dbset<filmtitle> filmtitles {get; set;}        Public dbset<producer> producers {get; set;}        Public dbset<role> Roles {get; set;}        Public Dvdcontext ()            : Base ("dvdconnectionstring")        {        }    }

You need to configure dvdconnectionstring in app. Config,

  <connectionStrings>    <add name= "dvdconnectionstring" connectionstring= "Data source=|  DataDirectory| DVDDATABASE.SDF, "providername=" system.data.sqlserverce.4.0 "/>  </connectionStrings>

The above configuration file has a DataDirectory, which indicates the directory in which the database resides. Typically, the database is placed in a special location, such as a user folder, so that it is not deleted when the program is unloaded. We need to set the DataDirectory when the program starts, for example:

        static void Main (string[] args)        {            //Set Database location:            AppDomain.CurrentDomain.SetData ("DataDirectory ", Environment.getfolderpath (Environment.SpecialFolder.ApplicationData));        }

Here's how to build the current database:

    using (Dvdcontext db = new Dvdcontext ())    {        if (!db. Database.exists ())        {            db. Database.create ();        }    }

We can use LINQ to make the database to be used for additional pruning and checking operations.

Thank you for reading, code click here to download.

Entity Framework Code first for SQL Compact

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.