linq to sql的一个问题

卿文刚 2016-12-09 09:19:28
entityframework中有两个实体类a,b,这两个类在数据库中都有对应的表,其中b继承a,其中b类中没有主键,b对应的数据表中的主键映射到a类的主键字段上。在b中有一个导航属性c(b为主键,c为依赖建)

执行以下代码是有异常:

dbContext.set<b>().include(b=>b.c);
dbContext.where(b=>b.Id == 1).ToList();

异常信息:
指定表达式的 ResultType 与要求的类型不兼容。表达式 ResultType 为“a”,但要求的类型为“b"

请教达人,Include的导航属性
...全文
1357 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 2017-06-16
  • 打赏
  • 举报
回复
引用 5 楼 liuyuehui110 的回复:
怎么通过dbContext表名 示范一下
Entities就是你的上下文类。 Entities db = new Entities(); db.Users.Where(....)
  • 打赏
  • 举报
回复
引用 4 楼 hanjun0612 的回复:
[quote=引用 3 楼 liuyuehui110 的回复:] 这里 public DbSet<U1> U1 { get; set; } public DbSet<U2> U2 { get; set; } 变量名 怎么和类名一样
这样当你 通过 dbContext.表名的时候,更加形象。ef默认就是这样的[/quote] 怎么通过dbContext表名 示范一下
正怒月神 2017-06-16
  • 打赏
  • 举报
回复
引用 3 楼 liuyuehui110 的回复:
这里 public DbSet<U1> U1 { get; set; } public DbSet<U2> U2 { get; set; } 变量名 怎么和类名一样
这样当你 通过 dbContext.表名的时候,更加形象。ef默认就是这样的
  • 打赏
  • 举报
回复
引用 2 楼 hanjun0612 的回复:
没有问题啊。 贴一下我的测试代码。

//上下文
public partial class Entities : DbContext
    {
        public Entities()
            : base("name=TestUserDataEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            
        }

        public DbSet<U1> U1 { get; set; }
        public DbSet<U2> U2 { get; set; }
    }
public class U
    {
//基类
        [Key]
        public int id { get; set; }
        public string name { get; set; }
    }

//U1表
    public partial class U1:U
    {
        public U1()
        {
            this.U2 = new HashSet<U2>();
        }
    
        public Nullable<int> input_Qty { get; set; }
        public Nullable<int> output_Qty { get; set; }
    
        public virtual ICollection<U2> U2 { get; set; }
    }
U2表
public partial class U2
    {
        [Key]
        public int Id { get; set; }
        public Nullable<int> Uid { get; set; }
        public string remark { get; set; }

        [ForeignKey("Uid")]
        public virtual U1 U1 { get; set; }
    }
调用:
protected void Button1_Click(object sender, EventArgs e)
        {
            Entities db = new Entities();
//这里Include我直接使用重载的string参数,表名就可以了
            var q = db.Set<U1>().Include("U2").Where(x=>x.id==1).ToList();
            
        }
这里 public DbSet<U1> U1 { get; set; } public DbSet<U2> U2 { get; set; } 变量名 怎么和类名一样
  • 打赏
  • 举报
回复
引用 6 楼 hanjun0612 的回复:
[quote=引用 5 楼 liuyuehui110 的回复:] 怎么通过dbContext表名 示范一下
Entities就是你的上下文类。 Entities db = new Entities(); db.Users.Where(....) l[/quote] 了解
正怒月神 2016-12-09
  • 打赏
  • 举报
回复
没有问题啊。 贴一下我的测试代码。

//上下文
public partial class Entities : DbContext
    {
        public Entities()
            : base("name=TestUserDataEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            
        }

        public DbSet<U1> U1 { get; set; }
        public DbSet<U2> U2 { get; set; }
    }
public class U
    {
//基类
        [Key]
        public int id { get; set; }
        public string name { get; set; }
    }

//U1表
    public partial class U1:U
    {
        public U1()
        {
            this.U2 = new HashSet<U2>();
        }
    
        public Nullable<int> input_Qty { get; set; }
        public Nullable<int> output_Qty { get; set; }
    
        public virtual ICollection<U2> U2 { get; set; }
    }
U2表
public partial class U2
    {
        [Key]
        public int Id { get; set; }
        public Nullable<int> Uid { get; set; }
        public string remark { get; set; }

        [ForeignKey("Uid")]
        public virtual U1 U1 { get; set; }
    }
调用:
protected void Button1_Click(object sender, EventArgs e)
        {
            Entities db = new Entities();
//这里Include我直接使用重载的string参数,表名就可以了
            var q = db.Set<U1>().Include("U2").Where(x=>x.id==1).ToList();
            
        }
卿文刚 2016-12-09
  • 打赏
  • 举报
回复
有人知道这个问题怎么解决线

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧