Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用DbContext关联列_C#_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# 使用DbContext关联列

C# 使用DbContext关联列,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我不知道如何使用include方法将一列与另一个表关联起来 我有以下型号 public class Fleet { [Key] public Guid OwnerId { get; set; } public String ownerName { get; set; } public virtual ICollection<UserAccount> UserAccount { get; set; } } public class UserAccou

我不知道如何使用include方法将一列与另一个表关联起来

我有以下型号

public class Fleet
{
    [Key]
    public Guid OwnerId { get; set; }
    public String ownerName { get; set; }
    public virtual ICollection<UserAccount> UserAccount { get; set; }

}

public class UserAccount
    {
        [Key]
        [Display(Name="UserID")] 
        public Guid UserID { get; set; }     
        public String UserName { get; set; }  
        public Guid SelectedFleet {   get; set; }
        public Guid? PrimaryFleet { get; set; }
        public String Password { get; set; }
        public virtual Fleet Fleet { get; set; }
    } 
问题是,当我尝试包含震源组时,我的记录返回null,我知道我做错了什么,因为我甚至不知道如何告诉include哪个列应该与我尝试的表相关
d=>d.fleet.OwnerId==d.SelectedFleet
,但我收到一个错误,告诉我这不是inlcude的用法

var v = dc.UserAccounts.Where(a => a.UserName == model.UserName).Include(d => d.Fleet).SingleOrDefault();

您不能“配置”
包括
。使用命名约定、属性和Fluent API提前进行模型配置。我看不到属性和Fluent API,所以默认情况下,您应该在
UserAccount
表中有
Fleet\u Id
列,是吗?但是有两个列与Fleet相关,primaryfleet列和selectedfleet,是否可以同时具有这两个列?而不是根据显示的类约定/属性。你有流畅的配置代码吗?@IvanStoev没有,我没有类似这样的流畅配置代码<代码>受保护的重写void OnModelCreating(DbModelBuilder modelBuilder){modelBuilder.Conventions.Remove();modelBuilder.Entity().HasMany(c=>c.Instructors)。with many(i=>i.Courses)。Map(t=>t.MapLeftKey(“CourseID”).MapRightKey(“InstructorID”).ToTable(“CourseInstructor”);modelBuilder.Entity().MapToStoredProcedures();}我从一个示例中获取了这一点
var v = dc.UserAccounts.Where(a => a.UserName == model.UserName).Include(d => d.Fleet).SingleOrDefault();