Created
August 13, 2014 13:13
-
-
Save margusmartsepp/f9fcc9178600ca53acf6 to your computer and use it in GitHub Desktop.
Linq to sql code first example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<configuration> | |
<configSections> | |
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> | |
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> | |
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> | |
</configSections> | |
<startup> | |
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> | |
</startup> | |
<connectionStrings> | |
<add name="Program.CustomerContext" connectionString="Data Source=|DataDirectory|Program.CustomerContext.sdf" providerName="System.Data.SqlServerCe.4.0" /> | |
</connectionStrings> | |
<system.data> | |
<DbProviderFactories> | |
<remove invariant="System.Data.SqlServerCe.4.0" /> | |
<add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" /> | |
</DbProviderFactories> | |
</system.data> | |
<entityFramework> | |
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework"> | |
<parameters> | |
<parameter value="System.Data.SqlServerCe.4.0" /> | |
</parameters> | |
</defaultConnectionFactory> | |
<providers> | |
<provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" /> | |
</providers> | |
</entityFramework> | |
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<packages> | |
<package id="EntityFramework" version="6.1.1" targetFramework="net45" /> | |
<package id="EntityFramework.SqlServerCompact" version="6.1.1" targetFramework="net45" /> | |
<package id="Microsoft.SqlServer.Compact" version="4.0.8854.1" targetFramework="net45" /> | |
</packages> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ComponentModel.DataAnnotations; | |
using System.ComponentModel.DataAnnotations.Schema; | |
using System.Data.Entity; | |
using System.Linq; | |
namespace ConsoleApplication2 | |
{ | |
class Program | |
{ | |
[Table("CustomerTest")] | |
public class Customer | |
{ | |
[Key] | |
public int Id { get; set; } | |
public string FirstName { get; set; } | |
public int Status { get; set; } | |
} | |
public class CustomerContext : DbContext | |
{ | |
public CustomerContext(): base("name=Program.CustomerContext"){} | |
public DbSet<Customer> Customers { get; set; } | |
} | |
//PM> EntityFramework | |
//PM> Install-Package EntityFramework.SqlServerCompact | |
static void Main(string[] args) | |
{ | |
using (var db = new CustomerContext()) | |
{ | |
var item = new Customer {FirstName = "test", Status = 2}; | |
db.Customers.Add(item); | |
db.SaveChanges(); | |
var items = db.Customers.Where(o => (o.FirstName == "test" && o.Status > 1)); | |
Console.WriteLine(items.ToString()); | |
} | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment