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.配置结束,可以使用

  • 相关阅读:
    SQL Server用户和角色
    小草手把手教你 LabVIEW 串口仪器控制——VISA 串口配置
    数据库的概念
    域对象
    session
    cookie
    表单
    HttpServletResponse
    JavaWeb核心之Servlet
    Tomcat服务器
  • 原文地址:https://www.cnblogs.com/zhiguzhidao/p/12304595.html
Copyright © 2011-2022 走看看