Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.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# EF 4.3具有最小导航属性的一对多关系_C#_Entity Framework 4 - Fatal编程技术网

C# EF 4.3具有最小导航属性的一对多关系

C# EF 4.3具有最小导航属性的一对多关系,c#,entity-framework-4,C#,Entity Framework 4,我有以下情况: public class Person { public string Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public ICollection<PhoneNumber> PhoneNumbers { get; set; } } public class PhoneNumber {

我有以下情况:

public class Person
{
    public string Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public ICollection<PhoneNumber> PhoneNumbers { get; set; }
}

public class PhoneNumber
{
    public int Id { get; set; }
    public string Number { get; set; }
    public string Type { get; set; }
}
我有两个类也实现了EntityTypeConfiguration

public class PhoneNumberConfiguration : EntityTypeConfiguration<PhoneNumber>
{
    public PhoneNumberConfiguration()
    {
        ToTable(TableName);

        Property(e => e.Id).HasColumnName("PhoneNumberId");
        Property(e => e.Value).HasColumnName("PhoneNumber");
        Property(e => e.PhoneType).HasColumnName("Type");
    }
}

public class ContactConfiguration : EntityTypeConfiguration<Person>
{
    public ContactConfiguration()
    {
        ToTable("Contacts");

        Property(contact => contact.Id)
            .HasColumnName("ContactId");
    }
}
公共类电话号码配置:EntityTypeConfiguration
{
公用电话号码配置()
{
ToTable(表名);
属性(e=>e.Id).HasColumnName(“PhoneNumberId”);
属性(e=>e.Value);
属性(e=>e.PhoneType).HasColumnName(“类型”);
}
}
公共类ContactConfiguration:EntityTypeConfiguration
{
公共联系人配置()
{
可更换的(“触点”);
属性(contact=>contact.Id)
.HasColumnName(“联系人ID”);
}
}
由此,我可以使用EF 4.3的fluent API来指定电话号码和人员之间的外键位于存储在名为ContactId的字段中的电话号码表中吗


谢谢

是的,这是可能的。在
ContactConfiguration
构造函数中添加映射:

HasMany(contact => contact.PhoneNumbers)
    .WithRequired()
    .Map(map => map.MapKey("ContactId"));

是的,有可能。在
ContactConfiguration
构造函数中添加映射:

HasMany(contact => contact.PhoneNumbers)
    .WithRequired()
    .Map(map => map.MapKey("ContactId"));