实体框架.net核心加入多个数据库上下文

时间:2017-03-13 20:28:29

标签: entity-framework linq-to-sql asp.net-core asp.net-core-mvc .net-core

我正在尝试执行以下查询

 var resultStu1 = await (from st in _context.tbl_Student

                                            join de in _contextCNS.View_SrmCounselling_GetDegrees on st.DegreeId equals de.ID
                                            join ex in _context.tbl_Student_Exam_Mapping_Table on st.ApplicantId equals ex.ApplicantId

                                            where st.ApplicantId == AppId
                                            select new
                                            {
                                                ApplicantId = st.ApplicantId,
                                                Image = st.Photo,
                                                FullName = st.FirstName.Trim() + " " + st.LastName.Trim(),
                                                Signature = st.Signature,
                                                Rank = ex.Rank,
                                                Category = st.Category,
                                                ContactNumber = st.Mobile.Substring(0, 1) + "*****" + st.Mobile.Substring(st.Mobile.Length - 1, 1),
                                                Email = String.Format(st.Email.ToString().Substring(0, 1) + "********" + (st.Email).ToString().Substring(st.Email.Length - 4, 4)),
                                                de.DegreeName.Trim()
                                            }).ToListAsync();

在Controller构造函数中

 public CommonController(EntityModelContext context, EntityModelContextCNS contextCNS)
    {
        _context = context;
        _contextCNS = contextCNS;
    }

我添加了两个DB Context

 public class EntityModelContext : DbContext
{


    public EntityModelContext(DbContextOptions<EntityModelContext> options) : base(options)
    {

    }
    public EntityModelContext() { }
  public DbSet<Student> tbl_Student { get; set; }
      public DbSet<StudentExamMapping> tbl_Student_Exam_Mapping_Table { get; set; }
}

}

public class EntityModelContextCNS : DbContext
{


    public EntityModelContextCNS(DbContextOptions<EntityModelContextCNS> options) : base(options)
    {

    }
    public EntityModelContextCNS() { }

    public DbSet<DegreeCNS> View_SrmCounselling_GetDegrees { get; set; }


}

这是我在Startup.cs中的ConfigureServices方法

 public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddMvc();
        services.AddCors(options => options.AddPolicy("AllowCors",
            builder =>
            {
                builder
                    .AllowAnyOrigin()

                .WithMethods("GET", "PUT", "POST", "DELETE")
                .AllowAnyHeader();
            })
    );
        services.AddEntityFramework()
        .AddDbContext<EntityModelContext>(options => options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"]))

       //services.AddEntityFramework()
       .AddDbContext<EntityModelContextCNS>(
           options => options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnectionCNS"]));

        //Inject an implementation of ISwaggerProvider with defaulted settings applied
        services.AddSwaggerGen();

        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    }

模特:

 public class Student
{
    public int Id { get; set; }
    public int ApplicantId { get; set; }
    public string Title { get; set; }
    public string Category { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleName { get; set; }
    public string Gender { get; set; }
    public string Email { get; set; }
    public string Mobile { get; set; }
    public string Address { get; set; }
    public string Photo { get; set; }
    public string Signature { get; set; }
    public string Biometric { get; set; }
    public Nullable<System.DateTime> CreatedOn { get; set; }
    public Nullable<System.DateTime> UpdatedOn { get; set; }
    public string PermanentAddressState { get; set; }
    public string TwelfthBoard { get; set; }
    public Nullable<int> FacultyId { get; set; }
    public Nullable<int> LevelId { get; set; }
    public Nullable<int> DegreeId { get; set; }
    public string UG_University { get; set; }
    public string TenthBoard { get; set; }
    public Nullable<int> SlotId { get; set; }

}
public class DegreeCNS
{
    [Key]
    public int ID { get; set; }
    public string DegreeName { get; set; } = "";
    public string Code { get; set; }

}
 public class StudentExamMapping
{
    public int Id { get; set; }
    public Nullable<int> ApplicantId { get; set; }
    public Nullable<int> ExamId { get; set; }
    public Nullable<System.DateTime> ExamDate { get; set; }
    public Nullable<decimal> Section1Marks { get; set; }
    public Nullable<decimal> Section2Marks { get; set; }
    public Nullable<decimal> Section3Marks { get; set; }
    public Nullable<int> Rank { get; set; }
    public Nullable<int> ScholarshipId { get; set; }

}

On执行resultStu1查询我得到ERRROR -------

  

System.ArgumentNullException:值不能为null。   参数名称:entityType      在Microsoft.EntityFrameworkCore.Utilities.Check.NotNull [T](T值,String parameterName)      在Microsoft.EntityFrameworkCore.SqlServerMetadataExtensions.SqlServer(IEntityType entityType)

请帮助!!!!

1 个答案:

答案 0 :(得分:0)

我注意到您加入了可以为空的列,请检查thisthis 或者其他来源解决&#34;加入可空的&#34;

基本上

您应该为可为空的

指定默认值

或者

忽略where子句

中值为null的记录