zoukankan      html  css  js  c++  java
  • 解决EF使用context.Database.SqlQuery时NotMapped属性列为空null的问题(转载)

    有时候我们要为EF中的Model加一个新属性,这个属性不是数据库中的字段,而是从其它表中关联出来的。EF中要标示一个列不是对应表中字段只需要加上NotMapped特性。要使用NotMapped,保证你的项目引用了System.ComponentModel.DataAnnotations.dll,NotMapped特性在命名空间System.ComponentModel.DataAnnotations.Schema下。比如我们有一个Employee实体:

    public class Employee
    {
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        [NotMapped]
        public string CustomerName { get; set; }
    }

    可能你会想到用context.Database.SqlQuery写一句原生的sql为CustomerName赋值,就像下面的代码:

    public List<Employee> GetEmployees()
    {
        using (MyContext context = new MyContext())
        {
            return context.Database.SqlQuery<Employee>("select E.EmployeeId, E.EmployeeName, C.CustomerName from Employee E left join Customer C on E.CustomerId = C.CustomerId").ToList();
        }
    }

    但是遗憾是上面代码字段CustomerName始终都为空。因为使用context.Database.SqlQuery虽然是传的sql语句,但是它和原生的ADO.NET还是不一样,它还是会走一下EF框架,用NotMapped标示的字段,EF会忽略掉这个字段,当生成sql语句也不会包含这个字段,无论你增,改,查都不会管这个字字段,所以读出来始终为空null值。下面介绍一种利用linq的变通的办法。

    public class EmployeeVM
    {
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public string CustomerName { get; set; }
    }

    调用EF并使用linq的left join读取属性:

    public List<Employee> GetEmployees()
    {
        using (MyContext context = new MyContext())
        {
            return context.Database.SqlQuery<EmployeeVM>("select E.EmployeeId, E.EmployeeName,
     C.CustomerName from Employee E left join Customer C on E.CustomerId = C.CustomerId")
        .Select(x=> new Employee(){
            EmployeeId = x.EmployeeId,
            EmployeeName = x.EmployeeName,
            CustomerName = x.CustomerName
            }).ToList();
        }
    }

    原文链接

  • 相关阅读:
    pom变成红橙色
    拉链表
    Newsgroups数据集研究
    python 安装第三方包
    概率图(三)CRF条件随机场
    概率图(二)马尔科夫随机场MRF
    LDA主题模型
    TF-IDF算法
    mysql 自定义排序顺序
    [转]解决a different object with the same identifier value was already associated with the session错误
  • 原文地址:https://www.cnblogs.com/OpenCoder/p/10177123.html
Copyright © 2011-2022 走看看