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 classfilmactorrole {[Key] Public intId {Get;Set; } [Required] Public intFilmtitleid {Get;Set; } [ForeignKey ("Filmtitleid")]         PublicFilmtitle Filetitle {Get;Set; } [Required] Public intActorid {Get;Set; } [ForeignKey ("Actorid")]         PublicActor actor {Get;Set; } [Required] Public intRoleid {Get;Set; } [ForeignKey ("Roleid")]         PublicRole role {Get;Set; }        [Required] [Stringlength (255)]         Public stringCharacter {Get;Set; }        [Required] [Stringlength (1024x768)]         Public stringDescription {Get;Set; } }

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

DvdContext.cs

     Public classDvdcontext:dbcontext { PublicDbset<actor> actors{Get;Set; }  PublicDbset<filmactorrole> Filmactorroles {Get;Set; }  PublicDbset<filmgenere> Filmgeneres {Get;Set; }  PublicDbset<filmtitle> Filmtitles {Get;Set; }  PublicDbset<producer> Producers {Get;Set; }  PublicDbset<role> Roles {Get;Set; }  PublicDvdcontext ():Base("dvdconnectionstring")        {        }    }

You need to configure dvdconnectionstring in app. Config,

  < connectionStrings >    <  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)        {            //  sets the location of database:             AppDomain.CurrentDomain.SetData ("DataDirectory", Environment.getfolderpath (Environment.SpecialFolder.ApplicationData));        }

Here's how to build the current database:

    using 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.