我是一名MVC新手,请在我身上轻松点。asp.net MVC 4 EntityType:EntitySet没有定义密钥
我在尝试添加迁移时遇到了两个错误。它们如下:
EntityType'Icon'没有定义键。定义此EntityType的关键字。
EntityType:EntitySet'图标'基于没有定义键的'Icon'类型。
我包括另一个模型里面的图标,就像这样:
public class Icon
{
public string IconName { get; set; }
public string IconColor { get; set; }
public int BackgroundXPos { get; set; }
public int BackgroundYPos { get; set; }
public string IconColorHover { get; set; }
public int BackgroundHoverXPos { get; set; }
public int BackgroundHoverYPos { get; set; }
}
public class GalleryThumbnail : CSSBoxModel
{
[DisplayName("Thumbnail Image Outline Color")]
public string ThumbnailImageOutlineColor { get; set; }
[DisplayName("Thumbnail Menu Font")]
public CSSFont ThumbnailMenuFont { get; set; }
[DisplayName("Thumbnail Icon Color")]
public Icon ThumbnailIconColor { get; set; }
}
如何在下面有什么不同这个地址类,这是工作:
public class Address
{
public String Adress1 { get; set; }
public String Adress2 { get; set; }
public String Adress3 { get; set; }
public String City { get; set; }
public String County { get; set; }
public String State { get; set; }
public String Zip { get; set; }
public String Country { get; set; }
}
[Table("UserProfile")] //Could be PP empolyee, Subscriber or Subscriber's customer
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public bool? Gender { get; set; }
public Address Address { get; set; } //billing address
public Address ShipAddress { get; set; }
}
我没有添加一个键后,我的图标或地址类,因为我不打算在我的数据库中存储特定的数据。他们只是在其他类中使用。所以你是一个内向的ID而另一个不是?
我还没有创建公共DbSet图标{get;组; }在我的数据库上下文中。
你也可以告诉我,当你在另一个类中使用一个类时(或者在这些例子中的一个类内的类的实例)时,它被称为什么?
非常感谢!
感谢Amhed和Kamyar。由于我无法找到Icon对象的任何IEnumerable或ICollection,因此两者似乎都没有出现。我可以用[ComplexType]装饰物品来解决问题。我读过这样一个好主意,就是用这种方式装饰任何复杂类型,原因与本文中所述的类似于我的许多原因:http://weblogs.asp.net/manavi/archive/2010/12/11/entity -association映射与 - 代码优先部分-1-酮对一-associations.aspx – 2013-02-26 06:49:44