Oscar.Data.SqlClient.EF6 1.0.1

dotnet add package Oscar.Data.SqlClient.EF6 --version 1.0.1
                    
NuGet\Install-Package Oscar.Data.SqlClient.EF6 -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Oscar.Data.SqlClient.EF6" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Oscar.Data.SqlClient.EF6" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Oscar.Data.SqlClient.EF6" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Oscar.Data.SqlClient.EF6 --version 1.0.1
                    
#r "nuget: Oscar.Data.SqlClient.EF6, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Oscar.Data.SqlClient.EF6@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Oscar.Data.SqlClient.EF6&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Oscar.Data.SqlClient.EF6&version=1.0.1
                    
Install as a Cake Tool

快速开始

创建控制台程序

通过 Visual Studio 2015 或 2022 创建 .NET Framework 4.6.1 控制台程序

安装依赖库

  • EntityFramework version="6.5.1"
  • Oscar.Data.SqlClient version="4.2.32"
  • Oscar.Data.SqlClient.EF6" version="1.0.1"

编辑程序代码

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Data.OscarClient;
using System.Data.Entity.Migrations;
using System.Data.Entity.Migrations.History;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.ComponentModel.DataAnnotations.Schema;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (var db = new BloggingContext())
                {
                    //db.Database.CreateIfNotExists();
                    // Create and save a new Blog 

                    var name = "test_222";
                    var content1 = new byte[5] { 10, 20, 30, 40, 50 };
                    var content2 = new byte[5] { 60, 70, 80, 90, 100};

                    var blog = new Blog { BlogId = Guid.NewGuid(), Name = name };
                    var post1 = new Post { Title = "post1_111", Content = content1, Blog = blog };
                    var post2 = new Post { Title = "post2_111", Content = content2, Blog = blog };

                     //var billingdetail = 
                    var bankaccout = new BankAccount { BillingDetailId = 1, Owner = "China", Number = "001", BankName = "ABC", Swift = "UnionPay" };
                    var creditcard = new CreditCard { BillingDetailId = 2, Owner = "China", Number = "100", CardType = 1, ExpiryMonth = "08", ExpiryYear = "2020" };

                    db.Blogs.Add(blog);

                    db.Posts.Add(post1);
                    db.Posts.Add(post2);

                    db.BillingDetails.Add(bankaccout);
                    db.BillingDetails.Add(creditcard);

                    db.SaveChanges();

                    // Display all Blogs from the database 
                    var query = from b in db.Blogs
                                where b.Name == "test_222"
                                //orderby b.Name
                                select b;

                    var count = query.Count();
                    Console.WriteLine("All blogs in the database:");
                    foreach (var item in query)
                    {
                        Console.WriteLine(item.Name);
                        foreach(var post in item.Posts)
                        {
                            Console.WriteLine(post.Title+":" +post.Content);
                        }
                    }

                    //多态查询
                    IQueryable<BillingDetail> linq_bd = from b in db.BillingDetails select b;
                    List<BillingDetail> billingDetails = linq_bd.ToList();
                    foreach (var item in billingDetails)
                    {
                        Console.WriteLine("BillingDetail : " + item.BillingDetailId + ":" + item.Owner + ":" + item.Number);
                    }

                //    IQueryable<BankAccount> linq_ba = from b in db.BillingDetails.OfType<BankAccount>() select b;
                //    List<BankAccount> bankAccounts = linq_ba.ToList();
                //    foreach (var item in bankAccounts)
                //    {
                //        Console.WriteLine("BankAccount : " + item.BillingDetailId + ":" + item.Owner + ":"
                //            + item.Number + ":" + item.BankName + ":" + item.Swift);
                //    }

                //    IQueryable<CreditCard> linq_cc = from b in db.BillingDetails.OfType<CreditCard>() select b;
                //    List<CreditCard> creditCards = linq_cc.ToList();
                //    foreach (var item in creditCards)
                //    {
                //        Console.WriteLine("CreditCard : " + item.BillingDetailId + ":" + item.Owner + ":"
                //            + item.Number + ":" + item.CardType + ":" + item.ExpiryYear + ":" + item.ExpiryMonth);
                //    }

                   Console.WriteLine("Press any key to exit...");
                   Console.ReadKey();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }

    public class Blog
    {
        [Key]
        public Guid BlogId { get; set; }
        [MaxLength(100), StringLength(100)]
        public string Name { get; set; }
        [NotMapped]
        public string Author { get; set; }
        public virtual List<Post> Posts { get; set; }
    }
    
    public class Post
    {
        [Key]
        public int PostId { get; set; }
        [Required]
        public string Title { get; set; }
        [Column("Post_Content")]
        public byte[] Content { get; set; }

        [ForeignKey("Blog")]
        public Guid BlogId { get; set; }
        public virtual Blog Blog { get; set; }
    }

    public abstract class BillingDetail
    {
        public int BillingDetailId { get; set; }
        public string Owner { get; set; }
        public string Number { get; set; }
    }

    public class BankAccount : BillingDetail
    {
        public string BankName { get; set; }
        public string Swift { get; set; }
    }

    public class CreditCard : BillingDetail
    {
        public int CardType { get; set; }
        public string ExpiryMonth { get; set; }
        public string ExpiryYear { get; set; }
    }
    public class BloggingContext : DbContext
    {    
        public BloggingContext() : base("BloggingContext")
        {
            Database.SetInitializer<BloggingContext>(new MigrateDatabaseToLatestVersion<BloggingContext, OscarMigrationConfiguration>());
            // Database.SetInitializer<BloggingContext>(new BaseInitializer());
           // Database.SetInitializer(new MigrateDatabaseToLatestVersion<BloggingContext, OscarMigrationConfiguration>());
            //Database.SetInitializer<BloggingContext>(null);
        }

        //使用name=EFDemo的连接字符串
        public BloggingContext(string nameOrConnectionString) : base(nameOrConnectionString) { }

        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
        public DbSet<BillingDetail> BillingDetails { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            ///modelBuilder.Conventions.Remove(pl);
            //modelBuilder.Configurations.AddFromAssembly(typeof(BloggingContext).Assembly);
            //modelBuilder.HasDefaultSchema("testef");
            //modelBuilder.Entity<HistoryRow>().ToTable("__MigrationHistory", "SYSDBA");
            //modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            //modelBuilder.Entity<Blog>().ToTable("Blogs", "testef");
            //modelBuilder.Entity<Post>().ToTable("Posts", "testef");

            //约定
            //modelBuilder.Types().Configure(f => f.ToTable("EF6_" + f.ClrType));

            //调用自定义约定
            //modelBuilder.Conventions.Add(new String2TextConvention());

            //fluent api 概念实体和存储实体之间的映射关系
            //modelBuilder.Entity<Blog>().HasKey(c=>c.BlogId); //设置实体Blog主键
            //modelBuilder.Entity<Post>().HasKey(c=>c.PostId);// 设置实体Post主键

            //modelBuilder.Entity<Blog>().ToTable("Blog", "SYSDBA");//设置实体对应的存储表的名字、模式名

            //修改实体对应的表的列的名字,类似起别名
            //modelBuilder.Entity<Post>().Property(t => t.Content).HasColumnName("Post_Content");

            //modelBuilder.Entity<Blog>().Property(b => b.Name).IsRequired();//设置字段非空约束
            //modelBuilder.Entity<Blog>().Property(b => b.Name).HasMaxLength(1000);//设置字段长度
            //modelBuilder.Entity<Blog>().Property(b => b.Name).HasColumnType("varchar");//设置字段类型
        }
    }


    public class BaseInitializer : DropCreateDatabaseIfModelChanges<BloggingContext>
    {
        protected override void Seed(BloggingContext context)
        {
             base.Seed(context);
            // base.InitializeDatabase(context);
            //var blogs = new List<Blog>
            //{
            //        new Blog { BlogId = Guid.NewGuid(), Name = "name_1" },
            //        new Blog { BlogId = Guid.NewGuid(), Name = "name_2" }
            //};
            //blogs.ForEach(s => context.Blogs.Add(s));
            //context.SaveChanges();
        }
    }

    public class OscarMigrationConfiguration : DbMigrationsConfiguration<BloggingContext>
    {
        public OscarMigrationConfiguration()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
            //SetSqlGenerator("System.Data.OscarClient", new OscarMigrationSqlGenerator());
        }

        protected override void Seed(BloggingContext context)
        {
           // base.Seed(context);
        }
    }

    //自定义约定
    public class String2TextConvention : Convention
    {
        public String2TextConvention()
        {
            this.Properties<String>().Configure(c => c.HasColumnType("text"));
        }

    }
}

App.config 配置示例

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <system.data>
    <DbProviderFactories>
      
      <remove invariant="Oscar.Data.SqlClient" />
      <add description=".Net Framework Data Provider for ShenTong" invariant="Oscar.Data.SqlClient" name="ShenTong Data Provider" type="System.Data.OscarClient.OscarFactory, Oscar.Data.SqlClient" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="BloggingContext" connectionString="server=10.1.1.101;Database=OSRDB;UID=SYSDBA;PWD=Szoscar@55" providerName="Oscar.Data.SqlClient" />
  </connectionStrings>
  <entityFramework codeConfigurationType="System.Data.OscarClient.OscarEFConfiguration, Oscar.Data.SqlClient.EF6">
    <providers>
      
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="Oscar.Data.SqlClient" type="System.Data.OscarClient.OscarServices, Oscar.Data.SqlClient.EF6" />
    </providers>
  </entityFramework>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Oscar.Data.SqlClient" publicKeyToken="c769aeeb30786f39" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.32.0" newVersion="4.2.32.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

运行程序

控制台执行结果

All blogs in the database:
test_222
post1_111:System.Byte[]
post2_111:System.Byte[]
BillingDetail : 1:China:001
BillingDetail : 2:China:100
Press any key to exit...
Product Compatible and additional computed target framework versions.
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 104 4/8/2026