Entity framework 如何首先使用Entity Framework(EF)4.1代码将一个类的多个属性绑定到另一个类,以便与Upshot.js一起使用?
我有一个场景,需要首先使用EntityFramework4.1代码将两个属性从一个类绑定到另一个类。(供参考,Upshot.js正在使用此模型,以便在使用Knockout.js 2.1的单页应用程序中使用) 通常,我会做如下事情:Entity framework 如何首先使用Entity Framework(EF)4.1代码将一个类的多个属性绑定到另一个类,以便与Upshot.js一起使用?,entity-framework,ef-code-first,knockout.js,knockout-2.0,upshot,Entity Framework,Ef Code First,Knockout.js,Knockout 2.0,Upshot,我有一个场景,需要首先使用EntityFramework4.1代码将两个属性从一个类绑定到另一个类。(供参考,Upshot.js正在使用此模型,以便在使用Knockout.js 2.1的单页应用程序中使用) 通常,我会做如下事情: public class Person { [Key] public int PersonId { get; set; } public string FirstName { get; set; }
public class Person
{
[Key]
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Address HomeAddress { get; set; }
public Address OfficeAddress { get; set; }
}
public class Address
{
[Key]
public int AddressId { get; set; }
public string StreetAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
}
{"Unable to retrieve association information for association 'KnockoutTest.Models.Person_HomeAddress'. Only models that include foreign key information are supported. See Entity Framework documentation for details on creating models that include foreign key information."}
这允许HomeAddress和OfficeAddress都引用Address类的实例
请注意,这不是我真正的数据模型。它仅用于说明目的。在现实生活中,我可能会使用ICollection。不幸的是,对于这种特殊情况,它是不可行的,我确实需要维护从一个类到另一个类的多个引用
在这个特定的场景中,一个人也可能在没有定义任何地址的情况下生存。地址也有可能在没有人的情况下存在。(如前所述,此数据模型只是一个示例。)
虽然这可以正确编译,我甚至可以创建和保存数据,但当upshot尝试使用此模型时,它会痛苦地抱怨
它给出一个(内部)异常,如下所示:
public class Person
{
[Key]
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Address HomeAddress { get; set; }
public Address OfficeAddress { get; set; }
}
public class Address
{
[Key]
public int AddressId { get; set; }
public string StreetAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
}
{"Unable to retrieve association information for association 'KnockoutTest.Models.Person_HomeAddress'. Only models that include foreign key information are supported. See Entity Framework documentation for details on creating models that include foreign key information."}
所以。。。我试图在DbContext类上设置外键信息,如
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>()
.HasOptional(m => m.HomeAddress);
modelBuilder.Entity<Person>()
.HasOptional(m => m.OfficeAddress);
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.has可选(m=>m.HomeAddress);
modelBuilder.Entity()
.has可选(m=>m.OfficeAddress);
}
没有快乐!这仍然给了我同样的例外
我不知道如何设置外键关联,以便一个类上的多个属性可以引用另一个类——至少不是以一种结果会令人满意并暂时停止抱怨的方式
我做错了什么,如何修复它?您只需在模型类中引入标量外键属性:
public class Person
{
[Key]
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
[ForeignKey("HomeAddress")]
public int? HomeAddressId { get; set; }
public Address HomeAddress { get; set; }
[ForeignKey("OfficeAddress")]
public int? OfficeAddressId { get; set; }
public Address OfficeAddress { get; set; }
}
属性必须为空(int?
),因为您的关系是可选的(人可以不带地址而存在)