简体   繁体   English

实体框架6和导航属性问题

[英]Entity Framework 6 and navigational property issue

Im hoping to get your help here. 我希望在这里得到您的帮助。 Im very new to EF and am having some troubles. 我对EF非常陌生,遇到了一些麻烦。 I am using the Database First approach and have a database in Azure that I have to retreive data from. 我正在使用数据库优先方法,并且在Azure中有一个数据库,必须从中检索数据。

[DataContract]
[Table("A")]
public class AgencyDC
{

    [DataMember]
    [Key]
    public string AID { get; set; }
    public string AName { get; set; }
    public string GeneralEmailAddress { get; set; }
    public string WebsiteURL { get; set; }
    [DataMember]
    [ForeignKey("AID")]
    [IgnoreDataMember]
    public virtual AExtensionDC AExtension { get; set; }
}

[DataContract]
[Table("AExtension")]
public class AExtensionDC
{
    [DataMember]
    [Key]
    public string AID { get; set; }
    [DataMember]
    public bool? IsActive { get; set; }
    public bool? IsOptedOut { get; set; }
    public DateTime? LastUpdated { get; set; }
}

I am trying to use EF6 to retreive my records using DBSets in my context.. 我正在尝试使用EF6在上下文中使用DBSet检索记录。

public List<ADataCcontract> GetAllAs()
    {
        using (AContext _aCtx = new AContext())
        {                
            var mylist = _aCtx.A.Include("AExtension").ToList();
            return mylist;
        }            
    }

Now, I should be getting back 547 records back with only 1 of them having the AExtension navigational property having content within. 现在,我应该找回547条记录,其中只有1条具有AExtension导航属性,其中包含内容。 The other 546 records should contain NULL. 其他546条记录应包含NULL。 However, for some reason, I am only getting what appears to be a record that has a match in both tables. 但是,由于某种原因,我只能得到似乎在两个表中都匹配的记录。 In SQL speak, I kind of just want a left join so that I return ALL rows from AE entity and OPTIONALLY matches in AE. 用SQL来说,我只是想要一个左联接,以便我从AE实体返回所有行,并在AE中返回OPTIONALLY匹配项。

I hope this makes sense. 我希望这是有道理的。

If possible, if you have a fix, could you please post an example I could referent? 如果可能的话,如果您有修复程序,请提供一个我可以参考的示例吗? I am really stuck. 我真的被困住了。

I think this SO Answer might get you most of the way? 我认为这个SO Answer可能会让您受益匪浅? https://stackoverflow.com/a/4299667/78551 https://stackoverflow.com/a/4299667/78551

Basically Include does a left outer join or left join as 'outer' is actually optional in SQL. 基本上,Include包含左外部联接或左联接,因为“外部”实际上在SQL中是可选的。

A left join / inner join will be performed by ´.Include´ if your fields have/lack of nullability. 如果您的字段具有可空性,则左连接/内部连接将由“ .include”执行。

To review your query put a breakpoint and check this value: 要查看您的查询,请设置一个断点并检查此值:

  var myQuery = _aCtx.A.Include("AExtension").ToTraceString();

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

相关问题 实体框架Include()返回null导航属性 - Entity Framework Include() returns null navigational property 实体框架-仅加载导航属性中的某些行 - Entity Framework - Loading only certain rows in navigational property 实体框架 - 为集合中的成员加载特定的导航属性 - Entity Framework - Loading specific Navigational Property for members in collection 我如何模拟实体框架的导航财产情报? - How Do I Mock Entity Framework's Navigational Property Intelligence? 首先 Enitify Framework 代码:使用所需的相关导航属性更新实体 - Enitify Framework code first: Updating Entity With required related navigational property 实体框架自定义导航属性 - Entity Framework Custom Navigational Properties 实体框架导航属性记录从一个记录移动到另一个记录“等待操作超时” - Entity Framework Navigational property record moving from one record to another “Wait operation timed out” Entity Framework 6代码优先中的多维导航属性 - Multidimensional navigational properties in Entity Framework 6 Code First 实体框架中的Restsharp和关系/导航属性 - Restsharp and relations / navigational properties in Entity Framework 使用多对多导航属性更新EF中的实体 - Update entity in EF with many to many navigational property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM