EF数据库首先使用odata asp核心

时间:2019-02-20 18:27:55

标签: entity-framework asp.net-core odata

受此链接启发 是否可以仅对db的datacontext应用动态映射?无需手动重新定义所有实体集             EdmEntitySet学校= container.AddEntitySet(“学校”,学生); 我很想用多个数据源连接字符串进行查询 就像直接从Entityfile上下文获取edm 我的原则是在数据库EF生成的模型创建中具有与许多数据上下文相关的多个连接字符串,并且我想针对每个模型映射odata查询

命名空间KmCore.Model {     公共局部类DataContext:DbContext     {         公共DataContext()         {         }

    public DataContext(DbContextOptions<DataContext> options)
        : base(options)
    {
    }

    public virtual DbSet<V_KM_MO_MENU> V_KM_MO_MENU { get; set; }
    public virtual DbSet<V_KM_WEB_MENU_UTILISATEURS> V_KM_WEB_MENU_UTILISATEURS { get; set; }


    //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    //{
    //    if (!optionsBuilder.IsConfigured)
    //    {
    //        optionsBuilder.UseSqlServer("name=SI_KM");
    //    }
    //}

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var builder = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json");

        var configuration = builder.Build();
        var connectionString = configuration.GetConnectionString("SI_KM");
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer(
                connectionString

                );
        }
    }

    public virtual DbSet<AR_ABERRANTES> AR_ABERRANTES { get; set; }
    public virtual DbSet<AR_DJU_CHAUFFAGISTE_A> AR_DJU_CHAUFFAGISTE_A { get; set; }
    public virtual DbSet<AR_DJU_CHAUFFAGISTE_S> AR_DJU_CHAUFFAGISTE_S { get; set; }
    public virtual DbSet<AR_DJU_CLIMATICIEN_A> AR_DJU_CLIMATICIEN_A { get; set; }
    public virtual DbSet<AR_DJU_CLIMATICIEN_S> AR_DJU_CLIMATICIEN_S { get; set; }


        modelBuilder.Entity<TST_GENERATEUR_UNQKEY>(entity =>
        {
            entity.HasKey(e => new { e.TST_ID1, e.TST_ID2 });

            entity.Property(e => e.TST_ID1)
                .HasMaxLength(2)
                .IsUnicode(false);

            entity.Property(e => e.TST_ID2)
                .HasMaxLength(10)
                .IsUnicode(false);

            entity.Property(e => e.TST_BINARY).HasMaxLength(50);

            entity.Property(e => e.TST_BINARY_NN)
                .IsRequired()
                .HasMaxLength(50);

            entity.Property(e => e.TST_CHAR)
                .HasMaxLength(10)
                .IsUnicode(false);

            entity.Property(e => e.TST_CHAR_NN)
                .IsRequired()
                .HasMaxLength(10)
                .IsUnicode(false);


                .IsRequired()
                .HasMaxLength(50);

            entity.Property(e => e.TST_SMALLDATETIME).HasColumnType("smalldatetime");

            entity.Property(e => e.TST_SMALLDATETIME_NN).HasColumnType("smalldatetime");

            entity.Property(e => e.TST_SMALLMONEY).HasColumnType("smallmoney");

            entity.Property(e => e.TST_SMALLMONEY_NN).HasColumnType("smallmoney");

            entity.Property(e => e.TST_SQLVARIANT).HasColumnType("sql_variant");

            entity.Property(e => e.TST_SQLVARIANT_NN)
                .IsRequired()
                .HasColumnType("sql_variant");

            entity.Property(e => e.TST_TEXT).HasColumnType("text");

            entity.Property(e => e.TST_TEXT_NN)
                .IsRequired()
                .HasColumnType("text");

            entity.Property(e => e.TST_TIMESTAMP_NN).IsRowVersion();

            entity.Property(e => e.TST_VARBINARY).HasMaxLength(50);

            entity.Property(e => e.TST_VARBINARYMAX_NN).IsRequired();

            entity.Property(e => e.TST_VARBINARY_NN)
                .IsRequired()
                .HasMaxLength(50);

            entity.Property(e => e.TST_VARCHAR)
                .HasMaxLength(50)
                .IsUnicode(false);

            entity.Property(e => e.TST_VARCHARMAX).IsUnicode(false);

            entity.Property(e => e.TST_VARCHARMAX_NN)
                .IsRequired()
                .IsUnicode(false);

            entity.Property(e => e.TST_VARCHAR_NN)
                .IsRequired()
                .HasMaxLength(50)
                .IsUnicode(false);

            entity.Property(e => e.TST_XML).HasColumnType("xml");

            entity.Property(e => e.TST_XML_NN)
                .IsRequired()
                .HasColumnType("xml");
        });
    }
}

}`

这只是文件的一部分示例

0 个答案:

没有答案