导航属性XYZ的声明类型与指定导航的结果不兼容 [英] The declared type of navigation property XYZ is not compatible with the result of the specified navigation

查看:310
本文介绍了导航属性XYZ的声明类型与指定导航的结果不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模型

模板(Id,Name)
UserBody(Id,name)
EmployeeBody(Id,Name )
然后我有一个模板映射器,我将一个模板与许多
用户和雇员之一相关联。

Template (Id,Name) UserBody (Id, name) EmployeeBody (Id, Name) I then Have a template mappers where i associate a template with one of many users and employess.

TemplatesMaps(id,TemplateId,UserId ,EmployeeId)userid和employeeId可以为空

TemplatesMaps (id, TemplateId, UserId, EmployeeId) userid and employeeId are nullable

我需要一个TemplatesMaps,包含1个与多个Userbody.id和多个EmployeeBody.Id的
的示例

I need a TemplatesMaps to consist of 1 templateid mapping to many Userbody.id 's and many EmployeeBody.Id's Example

Id   TemplateId UserBodyId, EmployeeBodyId
1    1          1           Null
2    1          Null        Null  
3    2          4           Null
4    2          Null        5

我的代码如下

  public class UserBody 
  {
    [Key]
    public virtual int Id { get; set; }

    public virtual string Name { get; set; }
  }

  public class EmployeeBody
  {
    [Key]
    public virtual int Id { get; set; }

    public virtual string Name { get; set; }
  }

  public class Template
  {
    [Key]
    public virtual int Id { get; set; }

    public virtual string Name { get; set; }
  }

  public class TemplatesMaps
  {

    [Key]
    public virtual int Id { get; set; }

    public virtual Template Template { get; set; }

    public virtual ICollection<EmployeeBody> Employees { get; set; }

    public virtual ICollection<UserBody> Users { get; set; }
  }

  public class MyDbContext : DbContext
  { 
    public virtual IDbSet<EmployeeBody> EmployeeBody { get; set; }
    public virtual IDbSet<UserBody> UserBody { get; set; }
    public virtual IDbSet<Template> Templates { get; set; }
    public virtual IDbSet<TemplatesMaps> TemplatesMaps { get; set; }



    public MyDbContext() : base("Default")
    {
      Database.SetInitializer<TrawlerDbContext>(null);
    }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      modelBuilder.Entity<TemplatesMaps>().HasOptional(o => o.Employees).WithMany().Map(m => m.MapKey("EmployeeId"));
      modelBuilder.Entity<TemplatesMaps>().HasOptional(o => o.Usersus).WithMany().Map(m => m.MapKey("UserId"));
      base.OnModelCreating(modelBuilder);
    }


    //when i run the following i get the error  The declared type of navigation property XYZ is not compatible with the result of the specified navigation.
    var test = _templateMapperRepo.GetAll().Where(x => x.Template.Id == input.TemplateId).Include(x => x.Users).Include(xx => xx.Employees);


推荐答案

TemplatesMaps(Id,TemplateId ,UserId,EmployeeId)看起来更像一个连接表,因此需要一个不同的实体模型:

TemplatesMaps (Id, TemplateId, UserId, EmployeeId) looks more like a junction table, thus requires a different entity model:

public class TemplatesMaps
{
    [Key]
    public virtual int Id { get; set; }
    public virtual Template Template { get; set; }
    public virtual EmployeeBody Employee { get; set; }
    public virtual UserBody User { get; set; }
}

和设置

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<TemplatesMaps>().HasRequired(o => o.Template).WithMany().Map(m => m.MapKey("TemplateId"));
    modelBuilder.Entity<TemplatesMaps>().HasOptional(o => o.Employee).WithMany().Map(m => m.MapKey("EmployeeId"));
    modelBuilder.Entity<TemplatesMaps>().HasOptional(o => o.User).WithMany().Map(m => m.MapKey("UserId"));
    base.OnModelCreating(modelBuilder);
}

如果需要,您可以添加反向导航属性

If needed, you can add reverse navigation property

public virtual ICollection<TemplatesMaps> TemplateMaps { get; set; }

code> UserBody 和 EmployeeBody 类。只需确保相应地更新相应的 WithMany 配置,即 WithMany() => WithMany (e => e.TemplateMaps)

to any of the Template, UserBody and EmployeeBody classes. Just make sure to update the corresponding WithMany configuration accordingly, i.e. WithMany() => WithMany(e => e.TemplateMaps).

这篇关于导航属性XYZ的声明类型与指定导航的结果不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆