zoukankan      html  css  js  c++  java
  • Asp.Net Identity,Entity Framework 与 Sqlite

    1.Asp.Net Identity核心引用

    Microsoft.AspNet.Identity.Core   核心库,包含Identity的主要功能。

    Microsoft.AspNet.Identity.EntityFramework  主要包括ASP.NET Identity 的EF 部分的实现。

    Microsoft.AspNet.Identity.OWIN  ASP.NET Identity对OWIN 的支持。

    安装方式:创建Asp.Net Web 时选择身份验证或者使用NuGet安装

    通过在Package Manger Console输入如下命令来安装Identity:

    • Install-Package Microsoft.AspNet.Identity.EntityFramework

    • Install-Package Microsoft.AspNet.Identity.OWIN

    • Install-Package Microsoft.Owin.Host.SystemWeb

    2.Sqlite引用

    System.Data.SQLite

    System.Data.SQLite.Core

    System.Data.SQLite.EF6

    System.Data.SQLite.Linq

    SQLite.CodeFirst  使EF CodeFirst支持Sqlite

    Nuget内安装

    3.WebConfig配置,Nuget安装时有些没有进行完整配置,需要保证下列节点包含下列配置

    EF DbContext使用的连接字符串

    <connectionStrings>

    <add name="SQLiteConnection" providerName="System.Data.SQLite.EF6" connectionString="Data Source=|DataDirectory|Data.db3;Pooling=True;BinaryGuid=False" />

    </connectionStrings>

    EF内注册Sqlite相关provider

    <entityFramework>
    <providers>
    <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    </providers>
    </entityFramework>

    <system.data>
    <DbProviderFactories>
    <remove invariant="System.Data.SQLite.EF6" />
    <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
    <remove invariant="System.Data.SQLite" />
    <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>
    </DbProviderFactories>
    </system.data>

    4.ApplicationDbContext配置

    主要添加OnModelCreating:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    //Sqlite CodeFrist如果不存在数据库,则创建
    Database.SetInitializer(new SqliteCreateDatabaseIfNotExists<ApplicationDbContext>(modelBuilder));

    //使用基于Microsoft.AspNet.Identity.EntityFramework.IdentityUser的ApplicationUser时,IdentityUserRole与IdentityUserLogin没有主键,在此指定

    //解决“EntityType 'IdentityUserLogin' has no key defined”及“EntityType 'IdentityUserRole' has no key defined”错误

    modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.UserId, r.RoleId });
    modelBuilder.Entity<IdentityUserLogin>().HasKey(r => r.UserId);
    //base.OnModelCreating(modelBuilder);
    }

    5.配置结束,可以使用

  • 相关阅读:
    姐姐的vue(1)
    LeetCode 64. Minimum Path Sum 20170515
    LeetCode 56. 56. Merge Intervals 20170508
    LeetCode 26. Remove Duplicates from Sorted Array
    LeetCode 24. Swap Nodes in Pairs 20170424
    LeetCode 19. Remove Nth Node From End of List 20170417
    LeetCode No.9 Palindrome Number 20170410
    LeetCode No.8. String to Integer (atoi) 2017/4/10(补上一周)
    LeetCode No.7 Reverse Integer 2017/3/27
    LeetCode No.4 Median of Two Sorted Arrays 20170319
  • 原文地址:https://www.cnblogs.com/zhiguzhidao/p/12304595.html
Copyright © 2011-2022 走看看