Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.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
Asp.net mvc EF一对一关系不能选择包含的表数据_Asp.net Mvc_Entity Framework_Asp.net Mvc 4_Entity Framework 6 - Fatal编程技术网

Asp.net mvc EF一对一关系不能选择包含的表数据

Asp.net mvc EF一对一关系不能选择包含的表数据,asp.net-mvc,entity-framework,asp.net-mvc-4,entity-framework-6,Asp.net Mvc,Entity Framework,Asp.net Mvc 4,Entity Framework 6,我必须列出:提供者、信息 信息表包含提供者数据。当我想查看包含数据的提供者列表时,它会说信息包含为空 我的提供者表: public class provider { public int id { get; set; } [Display(Name = "Username")] [StringLength( maximumLength: 100, MinimumLength = 2, ErrorMessageResourc

我必须列出:提供者、信息

信息表包含提供者数据。当我想查看包含数据的提供者列表时,它会说信息包含为空

我的提供者表:

public class provider
{
    public int id { get; set; }

    [Display(Name = "Username")]
    [StringLength(
        maximumLength: 100,
        MinimumLength = 2,
        ErrorMessageResourceType = typeof(Resources.Messages),
        ErrorMessageResourceName = "StringLength")]
    [Required(
        AllowEmptyStrings = false,
        ErrorMessageResourceType = typeof(Resources.Messages),
        ErrorMessageResourceName = "Required")]
    public string username { get; set; }

    [Display(Name = "Password")]
    [StringLength(
        maximumLength: 100,
        MinimumLength = 2,
        ErrorMessageResourceType = typeof(Resources.Messages),
        ErrorMessageResourceName = "StringLength")]
    [Required(
        AllowEmptyStrings = false,
        ErrorMessageResourceType = typeof(Resources.Messages),
        ErrorMessageResourceName = "Required")]
    public string password { get; set; }

    [Display(Name = "Email")]
    [StringLength(
        maximumLength: 100,
        MinimumLength = 2,
        ErrorMessageResourceType = typeof(Resources.Messages),
        ErrorMessageResourceName = "StringLength")]
    public string email { get; set; }

    public virtual information information { get; set; }
}

资料表如下:

public class information
    {
        public int id { get; set; }
    [Display(Name = "Name")]
    [StringLength(
        maximumLength: 100,
        MinimumLength = 2,
        ErrorMessageResourceType = typeof(Resources.Messages),
        ErrorMessageResourceName = "StringLength")]
    [Required(
        AllowEmptyStrings = false,
        ErrorMessageResourceType = typeof(Resources.Messages),
        ErrorMessageResourceName = "Required")]
    public string name { get; set; }

    [Display(Name = "Tell")]
    [StringLength(
        maximumLength: 100,
        MinimumLength = 2,
        ErrorMessageResourceType = typeof(Resources.Messages),
        ErrorMessageResourceName = "StringLength")]
    [Required(
        AllowEmptyStrings = false,
        ErrorMessageResourceType = typeof(Resources.Messages),
        ErrorMessageResourceName = "Required")]
    public string tellphone { get; set; }

    [Display(Name = "Location")]
    public DbGeography location { get; set; }

    public string exproviderusername { get; set; }
    public virtual provider provider { get; set; }

}
和提供商地图:

public providerMap()
{
    this.HasKey(x => x.username);

    this.Property(x => x.id)
        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
和信息地图:

public informationMap()
        {
            this.Property(x => x.id)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            this.HasRequired(x => x.provider)
                .WithOptional(x => x.information)
                .WillCascadeOnDelete(true);
}
我的控制器索引选择为:

    var providers = db.providers.Include(p => p.information);
    return View(providers.ToList());

当我跟踪代码时,信息表为空。为什么它应该是空的?如何修复它?

您可以在provider类中添加一个字段

    [Key, ForeignKey("Information")]
    public class InformationId {get;set;}

它不会转到链接

您有一个1:0..1映射,并且使用实体框架,双方都需要共享同一个键。如果要使用
id
作为键,则不能使用
this.HasKey(x=>x.username)。如果您想为
提供程序
使用字符串键,则
信息
也需要使用字符串键。

谢谢您的评论,您正在使用DataAnnotations进行配置,但我是使用Fluent API进行配置的。我的地图就是这样做的。