1

我有以下课程:

房东

[Table("Landlord")]
public class Landlord : UserProfile
{
    public static int LandlordProfileViews { get; set; }

    // A Landlord can have many ResidentialProperties
    [ForeignKey("ResidentialPropertyId")]
    public virtual ICollection<ResidentialProperty> ResidentialProperties { get; set; }

}

住宅物业

[Table("ResidentialProperty")]
public class ResidentialProperty
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ResidentialPropertyId { get; set; }
    // ...

    // A ResidentialProperty has 1 Landlord
    [ForeignKey("UserId")]
    public int UserId { get; set; }
    public virtual UserProfile UserProfile { get; set; }
}

一个 Landlord 可以拥有多个 ResidentialProperties,因此关联是一对多的。我正在尝试使用该Seed()方法将测试数据添加到我的数据库中。我的问题是我不知道如何在方法中定义关系的多端。以下是我尝试过的:

var residentialProperties = new List<ResidentialProperty>
{
    // Property 1 associated with LandLord 1
    new ResidentialProperty { /* properties */ },
    // ...
 }

var landlords = new List<Landlord>
 {
    new Landlord { /* properties */ ResidentialProperties = residentialProperties.FirstOrDefault(x => x.ResidentialPropertyId == 1) },
    // ...
 }

给出“ResidentialProperties = residentialProperties.FirstOrDefault(x => x.ResidentialPropertyId == 1)无法将类型 ResidentialProperty 隐式转换为 ICollection < ResidentialProperty > 错误。

如何在 Seed() 方法中实现一对多关系?

编辑:

我在上下文类中添加了以下内容来尝试实现这种类型的关系:房东可以有许多住宅属性。一个 ResidentialProperty 只能有一个 Landlord

modelBuilder.Entity<Landlord>()
            .HasMany(x => x.ResidentialProperties)
            .WithRequired()
            .HasForeignKey(x => x.ResidentialPropertyId);

modelBuilder.Entity<ResidentialProperty>()
            .HasRequired(x => x.UserProfile);

我仍然收到此错误:

\tSystem.Data.Entity.Edm.EdmAssociationEnd: : 多重性在关系“Landlord_ResidentialProperties”中的角色“Landlord_ResidentialProperties_Target”中无效。因为从属角色是指关键属性,所以从属角色的多重性的上限必须是'1'。

仍然对我做错了什么感到茫然。

4

2 回答 2

3

您需要返回 ResidentalProperties 列表。您的查询ResidentialProperties = residentialProperties.FirstOrDefault(x => x.ResidentialPropertyId == 1)仅返回一个 ResidentialProperty 实体。做就是了ResidentialProperties = residentialProperties

编辑:您可以通过单一配置进行多对一设置。您还必须指定外键。

            //ResidentialProperty has 1 Landlord ,
            //Landlord has many ResidentialProperties
            modelBuilder.Entity<ResidentialProperty>().HasRequired(a=> a.UserProfile)
                .WithMany(c=> c.ResidentialProperties)
                .HasForeignKey(a=> a.UserId);
于 2013-02-14T01:21:48.297 回答
2

我认为这是您要求的模型关系。

public class Landlord : UserProfile
{
    [Key]
    public Guid Id {get;set;} //If you named this "LandlorId" you wouldn't need the [Key]

//this convention will wire up to the otherside    
public virtual ICollection<ResidentialProperty> ResidentialProperties { get; set; }

}

public class ResidentialProperty{
   [Key]
   public Guid Id {get;set;}

   //this convention will wire up to the otherside
   public LandLordId {get;set;}
   public Landlord Landlord {get;set;}
}
于 2013-02-17T18:28:10.247 回答