• EntityFramework 5.0 6.0 CodeFirst SqlCompact 4 sp1


    1, 安装sql server compact edition 4 sp1 Runtime
    2, 安装sql server ce tool box for to Management or create local DB.
     
    http://sqlcetoolbox.codeplex.com/
     
    3, 以往的 Entity Framework 操作方式也不一樣了,先上 NuGet 取得 Entity Framework.SqlServerCompact
     
    Code First with SQL CE  a sample project using Entity Framework 6 with code first. 
    http://www.codeproject.com/Articles/680116/Code-First-with-SQL-CE
     
     
    Entity Framework 6 & SQL Server Compact
    http://dotnet.dzone.com/users/erikej
     
    重要提示:配置文件中 configSections 要在  connectionStrings 节下面,不然会报错。
     1   <system.data>
     2     <DbProviderFactories>
     3       <remove invariant="System.Data.SqlServerCe.4.0" />
     4       <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"/>
     5     </DbProviderFactories>
     6   </system.data>
     7   <connectionStrings>
     8     <add name="ProductContext" connectionString="Data Source=MyDB.sdf" providerName="System.Data.SqlServerCE.4.0"/>
     9     <!--<add name="sqliteContext"
    10  connectionString="Data Source=E:\WHPCodeLiberary\temp\SqlLiteDBs\TestDB.s3db;Version=3;New=True;"
    11  providerName="System.Data.SQLite"
    12   />-->
    13   </connectionStrings>
    app.config for EF 5.0 -

     重要提示:配置文件中 configSections 要在  connectionStrings 节下面,不然会报错。

    <?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>
      <connectionStrings>
        <add name="CFSqlCe.Dal.DvdContext" 
             providerName="System.Data.SqlServerCe.4.0" 
             connectionString="Data Source=D:\DvdDatabase.sdf" />
      </connectionStrings>
      <entityFramework>
        <providers>
          <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
        </providers>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
          <parameters>
            <parameter value="System.Data.SqlServerCe.4.0" />
          </parameters>
        </defaultConnectionFactory>
      </entityFramework>
    </configuration>
    app.config for EF 6.0
        public class Category
        {
            public string CategoryId { get; set; }
            public string Name { get; set; }
    
            public virtual ICollection<Product> Products { get; set; }
    
        }
    
        public class Product
        {
            public int ProductId { get; set; }
            public string Name { get; set; }
            public string CategoryId { get; set; }
            public virtual Category Category { get; set; }
        }
     1     /// <summary>
     2     /// 4.create context
     3     /// </summary>
     4     public class ProductContext : DbContext
     5     {
     6         public DbSet<Category> Categories { get; set; }
     7         public DbSet<Product> Products { get; set; }
     8 
     9         protected override void OnModelCreating(DbModelBuilder modelBuilder)
    10         {
    11             modelBuilder.Entity<Product>().Property(s => s.Name).IsRequired();
    12         }
    13     }
    14 
    15     class Program
    16     {
    17         static void Main(string[] args)
    18         {
    19             Database.SetInitializer<ProductContext>(new DropCreateDatabaseIfModelChanges<ProductContext>());
    20 
    21             using (var db = new ProductContext())
    22             {
    23                 var food = db.Categories.Find("FOOD");
    24                 if (food == null)
    25                 {
    26                     food = new Category { CategoryId = "FOOD", Name = "Foods" };
    27                     db.Categories.Add(food);
    28                 }
    29                 Console.Write("Please enter a name for a new food: ");
    30                 var productName = Console.ReadLine();
    31                 var product = new Product { Name = productName, Category = food };
    32                 db.Products.Add(product);
    33 
    34                 
    35 
    36                 int recordsAffected = db.SaveChanges();
    37 
    38                 Console.WriteLine("Save {0} entities to the database,press any key to exit.", recordsAffected);
    39                 var allFoods = from p in db.Products
    40                                where p.CategoryId == "FOOD"
    41                                orderby p.Name
    42                                select p;
    43 
    44                 foreach (var item in allFoods)
    45                 {
    46                     Console.WriteLine("- {0}", item.Name);
    47                 }
    48                 Console.ReadKey();
    49             }
    50         }
    51     }
     
     
  • 相关阅读:
    在 VS14 上安装 Visual F# Power Tools
    关于sources.list和apt-get [转载]
    MATLAB中导入数据:importdata函数
    部分 CM11 系统 Android 平板执行植物大战僵尸 2 黑屏的解决的方法
    html5视频播放
    用GruntJS合并、压缩JS文件
    Linux/UNIX进程控制(1)
    TCP/IP协议,HTTP协议
    php 求水仙花数优化
    使用LAMP创建基于wordpress的个从博客站点
  • 原文地址:https://www.cnblogs.com/netact/p/2669123.html
Copyright © 2020-2023  润新知