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进行配置的。我的地图就是这样做的。