简体   繁体   English

实体框架多对多关系返回Null

[英]Entity Framework Many to Many Relationship returning Null

I developed and uploaded a web service to Azure using Entity Framework 6.1.3 with MVC design pattern. 我使用带有MVC设计模式的Entity Framework 6.1.3开发并上传了一个Web服务。

So let's imagine I have a Workshop that can have many Clients and a Client that can have many Workshops. 因此,让我们假设我有一个可以拥有许多客户的研讨会和一个可以拥有许多研讨会的客户。

So far my results have been null, empty values and some times correct values but without the relationship (no clients inside my workshop, and the other way around). 到目前为止,我的结果是空的,空值和有时正确的值,但没有关系(我的工作室内没有客户,反之亦然)。

This is what I have at this point: 这就是我现在所拥有的:

public class Workshop
{
    public Workshop()
    {
        this.Clients = new HashSet<Client>();
        this.ModuleSets = new HashSet<ModuleSet>();
    }

    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    public virtual ICollection<Client> Clients { get; set; }
    public virtual ICollection<ModuleSet> ModuleSets { get; set; }
}

public class Client
{
    public Client()
    {
        this.Workshops = new HashSet<Workshop>();
        this.Vehicles = new HashSet<Vehicle>();
    }

    public int Id { get; set; }
    [Required]
    public string Name { get; set; }

    public virtual ICollection<Workshop> Workshops { get; set; }
    public virtual ICollection<Vehicle> Vehicles { get; set; }
}

Yes I have more relations going on at the same time. 是的,我有更多的关系在同一时间发生。

Since that alone was not giving me anything, I added some Fluent Api, like this: 由于仅此一点没有给我任何东西,我添加了一些Fluent Api,如下所示:

modelBuilder.Entity<Workshop>().
             HasMany(c => c.Clients).
             WithMany(p => p.Workshops).
             Map(
              m =>
              {
                  m.MapLeftKey("Workshop_Id");
                  m.MapRightKey("Client_Id");
                  m.ToTable("WorkshopClients");
              });

The names that are shown are the ones that are in the table WorkshopClients (auto generated by entity framework). 显示的名称是WorkshopClients表中的名称(由实体框架自动生成)。

I also read this article to make sure I was doing the right thing when it came to Fluent API. 我还阅读了这篇文章,以确保我在使用Fluent API时做的正确。

How to define Many-to-Many relationship through Fluent API Entity Framework? 如何通过Fluent API实体框架定义多对多关系?

And this is my simple request on the client: 这是我对客户的简单要求:

var request = new RestRequest("api/Workshops") { Method = Method.GET };
var workshopList = await api.ExecuteAsync<List<Workshop>>(request);

API/Workshops method: API /研讨会方法:

// GET: api/Workshops
public IQueryable<Workshop> GetWorkshops()
{
    return db.Workshops;
}

It looks like you are not using lazy loading or that part is lost when you pass the data over the API. 看起来您没有使用延迟加载,或者当您通过API传递数据时该部分会丢失。 Make sure you tell your API to include the child objects: 确保告诉您的API包含子对象:

public IQueryable<Workshop> GetWorkshops()
{
    return db.Workshops
        .Include(w => w.Clients);
}

Note: You may need to add using System.Data.Entity; 注意:您可能需要using System.Data.Entity;添加using System.Data.Entity; to use the lambda version of Include , otherwise you can use the string version. 使用Include的lambda版本,否则可以使用字符串版本。

I would recommend keeping your mappings in a separate mapping file, but if you are going to do it in the OnModelCreating method then this should do what you need. 我建议将映射保存在单独的映射文件中,但如果要在OnModelCreating方法中执行此操作,那么这应该可以满足您的需要。

    modelBuilder.Entity<Workshop>()  
        .HasRequired(c => c.Clients)  //or HasOptional depending on your setup
        .WithMany(d => d.Workshop) 
        .HasForeignKey(d => new { d.ID });

    modelBuilder.Entity<Clients>()  
        .HasRequired(c => c.Workshop)  //same
        .WithMany(d => d.Clients) 
        .HasForeignKey(d => new { d.ID });

Also in both entities add this to your ID properties:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM